<commit_before>/* Copyright (c) 2000-2003, jMock.org. See LICENSE.txt */
package test.jmock.builder;

import org.jmock.Constraint;
import org.jmock.builder.Mock;
import org.jmock.builder.MockObjectTestCase;
import org.jmock.constraint.IsEqual;
import org.jmock.dynamic.CoreMock;
import org.jmock.dynamic.InvocationMocker;
import org.jmock.dynamic.LIFOInvocationDispatcher;
import org.jmock.dynamic.matcher.ArgumentsMatcher;
import org.jmock.dynamic.matcher.MethodNameMatcher;
import org.jmock.dynamic.stub.VoidStub;


public class DynamicMockExample extends MockObjectTestCase {
    public interface Market {
    	String[] listStocks();
    	int getPrice( String ticker );
    	void buyStock(String ticker, int quantity);
    }

    public class Agent {
    	Market market;
    	
        public Agent(Market market) {
        	this.market = market;
        }

        public void buyLowestPriceStock(int cost) {
        	String[] stocks = market.listStocks();
        	int cheapestPrice = Integer.MAX_VALUE;
        	String cheapestStock = null;
        	
        	for (int i = 0; i < stocks.length; ++i) {
	        	int price = market.getPrice(stocks[i]);
	        	if (price < cheapestPrice) {
	        		cheapestPrice = price;
	        		cheapestStock = stocks[i];
	        	}
        	}
        	market.buyStock(cheapestStock, cost / cheapestPrice);
        }

    }

    /* This demonstrates the core API, upon which the sugar methods are layered.
     */
    public void xtestCoreMockExample() {
    	CoreMock mockMarket = new CoreMock( Market.class, "mockMarket",
    										new LIFOInvocationDispatcher() );
    	Agent agent = new Agent( (Market)mockMarket.proxy() );
    	
    	InvocationMocker invocation = new InvocationMocker();
    	invocation.addMatcher( new MethodNameMatcher("buyStock") );
    	invocation.addMatcher( new ArgumentsMatcher( new Constraint[] {
			new IsEqual("IBM"), new IsEqual(new Integer(10))					
    	} ) );
    	invocation.setStub( new VoidStub() ); // Not really necessary as this is the default
    	mockMarket.add(invocation);
    	//Phew! That's why we need syntactic sugar!
    	
    	agent.buyLowestPriceStock(20);
    	
    	mockMarket.verify();
    }
    
    public void testBuilderExample() {
    	Mock mockMarket = new Mock(Market.class);
        Agent agent = new Agent((Market) mockMarket.proxy());
        
        mockMarket.method("listStocks").noParams().willReturn(new String[]{"IBM","ORCL"});
        mockMarket.method("getPrice").args(eq("IBM")).willReturn(10)
        	.expectOnce();
        mockMarket.method("getPrice").args(eq("ORCL")).willReturn(25)
        	.expectOnce();
        mockMarket.method("buyStock").args(eq("IBM"), eq(2)).isVoid()
        	.expectOnce();
        
        agent.buyLowestPriceStock(20);

        mockMarket.verify();
    }

    public void xtestDynaMockExample() {
        Mock mockMarket = new Mock(Market.class);<commit_msg>[sf][np] removed dead code<commit_after>/* Copyright (c) 2000-2003, jMock.org. See LICENSE.txt */
package test.jmock.builder;

import org.jmock.builder.Mock;
import org.jmock.builder.MockObjectTestCase;


public class DynamicMockExample extends MockObjectTestCase {
    public interface Market {
    	String[] listStocks();
    	int getPrice( String ticker );
    	void buyStock(String ticker, int quantity);
    }

    public class Agent {
    	Market market;
    	
        public Agent(Market market) {
        	this.market = market;
        }

        public void buyLowestPriceStock(int cost) {
        	String[] stocks = market.listStocks();
        	int cheapestPrice = Integer.MAX_VALUE;
        	String cheapestStock = null;
        	
        	for (int i = 0; i < stocks.length; ++i) {
	        	int price = market.getPrice(stocks[i]);
	        	if (price < cheapestPrice) {
	        		cheapestPrice = price;
	        		cheapestStock = stocks[i];
	        	}
        	}
        	market.buyStock(cheapestStock, cost / cheapestPrice);
        }

    }

    public void testBuilderExample() {
    	Mock mockMarket = new Mock(Market.class);
        Agent agent = new Agent((Market) mockMarket.proxy());
        
        mockMarket.method("listStocks").noParams().willReturn(new String[]{"IBM","ORCL"});
        mockMarket.method("getPrice").args(eq("IBM")).willReturn(10)
        	.expectOnce();
        mockMarket.method("getPrice").args(eq("ORCL")).willReturn(25)
        	.expectOnce();
        mockMarket.method("buyStock").args(eq("IBM"), eq(2)).isVoid()
        	.expectOnce();
        
        agent.buyLowestPriceStock(20);

        mockMarket.verify();
    }

    public void xtestDynaMockExample() {
        Mock mockMarket = new Mock(Market.class);
{'fraction_non_alphanumeric': 0.11872731155242366, 'fraction_numerical': 0.007687379884689302, 'mean_word_length': 2.653666146645866, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '5759322', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1384, 'n_words': 294}