<commit_before>/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockitousage.matchers;

import static org.mockito.Mockito.*;

import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;

@SuppressWarnings("unchecked")
public class ArgumentCaptorTest extends TestBase {

    public class Capture<T> extends ArgumentMatcher<T> {
        private Object argument;

        public boolean matches(Object argument) {
            this.argument = argument;
            return true;
        }

        public T getValue() {
            //TODO nice instanceof check ?
            return (T) argument;
        }
    }

    class Person {

        private final int age;

        public Person(int age) {
            this.age = age;
        }

        public int getAge() {
            return age;
        }
    }
    
    class Foo {
     
        private IMethods methods;
        
        public Foo(IMethods methods) {
            this.methods = methods;
        }

        public void bar() {
            Person person = new Person(12);
            methods.simpleMethod(person);
        }
    }

    private Object capture(Capture<Person> capturedArgument) {
        Mockito.argThat(capturedArgument);
        return null;
    }

    @Mock IMethods mock;

    //TODO decide on getting capture matcher into Mockito
    @Test
    public void shouldUseArgumentCaptorInEasyMockStyle() {
        Foo foo = new Foo(mock);
        foo.bar();
        Capture<Person> capturedArgument = new Capture<Person>();
        verify(mock).simpleMethod(capture(capturedArgument));
        assertEquals(12, capturedArgument.getValue().getAge());
    }
    
    @Ignore("assertion idea is not for current release I guess")
    @Test
    public void shouldUseAssertor() {
        Foo foo = new Foo(mock);
        foo.bar();
        verify(mock).simpleMethod(argThat(new Assertion<Person>() {
            public void asserts(Person person) {
                assertEquals(12, person.getAge());
            }
        }));
    }
    
    public static <T> T argThat(Assertion<T> assertion) {
        return null;
    }
    
    public interface Assertion<T> {
        void asserts(T object);
    }
}<commit_msg>moved a TODO to next release<commit_after>/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockitousage.matchers;

import static org.mockito.Mockito.*;

import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;

@SuppressWarnings("unchecked")
public class ArgumentCaptorTest extends TestBase {

    public class Capture<T> extends ArgumentMatcher<T> {
        private Object argument;

        public boolean matches(Object argument) {
            this.argument = argument;
            return true;
        }

        public T getValue() {
            //TODO after 1.7 nice instanceof check here?
            return (T) argument;
        }
    }

    class Person {

        private final int age;

        public Person(int age) {
            this.age = age;
        }

        public int getAge() {
            return age;
        }
    }
    
    class Foo {
     
        private IMethods methods;
        
        public Foo(IMethods methods) {
            this.methods = methods;
        }

        public void bar() {
            Person person = new Person(12);
            methods.simpleMethod(person);
        }
    }

    private Object capture(Capture<Person> capturedArgument) {
        Mockito.argThat(capturedArgument);
        return null;
    }

    @Mock IMethods mock;

    //TODO after 1.7 decide on getting capture matcher into Mockito
    @Test
    public void shouldUseArgumentCaptorInEasyMockStyle() {
        Foo foo = new Foo(mock);
        foo.bar();
        Capture<Person> capturedArgument = new Capture<Person>();
        verify(mock).simpleMethod(capture(capturedArgument));
        assertEquals(12, capturedArgument.getValue().getAge());
    }
    
    @Ignore("assertion idea is not for current release I guess")
    @Test
    public void shouldUseAssertor() {
        Foo foo = new Foo(mock);
        foo.bar();
        verify(mock).simpleMethod(argThat(new Assertion<Person>() {
            public void asserts(Person person) {
                assertEquals(12, person.getAge());
            }
        }));
    }
    
    public static <T> T argThat(Assertion<T> assertion) {
        return null;
    }
    
    public interface Assertion<T> {
        void asserts(T object);
    }
}
{'fraction_non_alphanumeric': 0.09235209235209235, 'fraction_numerical': 0.004947433518862091, 'mean_word_length': 2.1384217335058215, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1688709', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1304, 'n_words': 364}