<commit_before>/*******************************************************************************
 * Copyright (c) 2014, 2014 <NAME> and other Contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     <NAME> - initial API and implementation
 *******************************************************************************/
package com.googlecode.goclipse.tooling.oracle;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertFail;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;

import java.util.regex.Pattern;

import org.json.JSONException;
import org.junit.Test;

import com.googlecode.goclipse.tooling.CommonGoToolingTest;

import melnorme.lang.tooling.ops.FindDefinitionResult;
import melnorme.lang.tooling.ops.SourceLineColumnRange;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.MiscUtil;

public class GoOracleFindDefinitionOperation_Test extends CommonGoToolingTest {
	
	@Test
	public void testCreateProcessBuilder() throws Exception { testCreateProcessBuilder$(); }
	public void testCreateProcessBuilder$() throws Exception {
		GoOracleFindDefinitionOperation op = new GoOracleFindDefinitionOperation("gopath");
		
		ProcessBuilder pb;
		pb = op.createProcessBuilder(SAMPLE_GOEnv_1, SAMPLE_GOPATH_Entry.resolve("src/foobar/file.go"), 0);
		assertEquals(pb.command().get(4), "foobar");
		
		try {
			op.createProcessBuilder(SAMPLE_GOEnv_1, SAMPLE_GOPATH_Entry.resolve("not_on_src/foobar/file.go"), 0);
			assertFail();
		} catch (CommonException se) {
			assertTrue(se.getMessage().contains("file not in the Go environment"));
		}
	}
	
	@Test
	public void test() throws Exception { test$(); }
	public void test$() throws Exception {
		
		String LOC_ROOT = MiscUtil.OS_IS_WINDOWS ? "D:\\devel/" : "/devel/";
		
		testParseResult(fixPaths(getClassResourceAsString("oracle_result.var_ref.json"), LOC_ROOT),
			new FindDefinitionResult(loc(LOC_ROOT+"go-workspace\\src\\github.com\\user\\newmath\\sqrt.go"), 
				new SourceLineColumnRange(5, 6), null)
			);
		
		testParseResult(fixPaths(getClassResourceAsString("oracle_result.type1_ref.json"), LOC_ROOT),
			new FindDefinitionResult(loc(LOC_ROOT+"DDT\\_runtime\\GoTest\\src\\other\\blah.go"),
				new SourceLineColumnRange(16, 6), null)
			);
		
		testParseResult(getClassResourceAsString("oracle_result.type2_def.json"), 
			null);
		
		testParseResult(getClassResourceAsString("oracle_result.type3_anon.json"), 
			null);
	}
	
	protected String fixPaths(String string, String pathRpl) {
		return string.replaceAll(Pattern.quote("##ROOT_LOC##"), pathRpl);
	}
	
	protected void testParseResult(String toolOutput, FindDefinitionResult expectedResult) throws JSONException,
			CommonException {
		GoOracleFindDefinitionOperation op = new GoOracleFindDefinitionOperation("gopath");
		
		try {
			FindDefinitionResult result = op.parseJsonResult(toolOutput);
			assertAreEqual(result, expectedResult);
		} catch(CommonException ce) {
			assertTrue(expectedResult == null);
		}<commit_msg>Properly fix tests on Windows this time...<commit_after>/*******************************************************************************
 * Copyright (c) 2014, 2014 <NAME> and other Contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     <NAME> - initial API and implementation
 *******************************************************************************/
package com.googlecode.goclipse.tooling.oracle;

import static melnorme.utilbox.core.Assert.AssertNamespace.assertFail;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;

import org.json.JSONException;
import org.junit.Test;

import com.googlecode.goclipse.tooling.CommonGoToolingTest;

import melnorme.lang.tooling.ops.FindDefinitionResult;
import melnorme.lang.tooling.ops.SourceLineColumnRange;
import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.MiscUtil;
import melnorme.utilbox.misc.StringUtil;

public class GoOracleFindDefinitionOperation_Test extends CommonGoToolingTest {
	
	@Test
	public void testCreateProcessBuilder() throws Exception { testCreateProcessBuilder$(); }
	public void testCreateProcessBuilder$() throws Exception {
		GoOracleFindDefinitionOperation op = new GoOracleFindDefinitionOperation("gopath");
		
		ProcessBuilder pb;
		pb = op.createProcessBuilder(SAMPLE_GOEnv_1, SAMPLE_GOPATH_Entry.resolve("src/foobar/file.go"), 0);
		assertEquals(pb.command().get(4), "foobar");
		
		try {
			op.createProcessBuilder(SAMPLE_GOEnv_1, SAMPLE_GOPATH_Entry.resolve("not_on_src/foobar/file.go"), 0);
			assertFail();
		} catch (CommonException se) {
			assertTrue(se.getMessage().contains("file not in the Go environment"));
		}
	}
	
	@Test
	public void test() throws Exception { test$(); }
	public void test$() throws Exception {
		
		String LOC_ROOT = MiscUtil.OS_IS_WINDOWS ? "D:\\devel/" : "/devel/";
		
		testParseResult(fixPaths(getClassResourceAsString("oracle_result.var_ref.json")),
			new FindDefinitionResult(loc(LOC_ROOT+"go-workspace\\src\\github.com\\user\\newmath\\sqrt.go"), 
				new SourceLineColumnRange(5, 6), null)
			);
		
		testParseResult(fixPaths(getClassResourceAsString("oracle_result.type1_ref.json")),
			new FindDefinitionResult(loc(LOC_ROOT+"DDT\\_runtime\\GoTest\\src\\other\\blah.go"),
				new SourceLineColumnRange(16, 6), null)
			);
		
		testParseResult(getClassResourceAsString("oracle_result.type2_def.json"), 
			null);
		
		testParseResult(getClassResourceAsString("oracle_result.type3_anon.json"), 
			null);
	}
	
	protected String fixPaths(String string) {
		String pathRpl = MiscUtil.OS_IS_WINDOWS ? "D:\\\\devel/" : "/devel/";
		return StringUtil.replaceAll(string, "##ROOT_LOC##", pathRpl);
	}
	
	protected void testParseResult(String toolOutput, FindDefinitionResult expectedResult) throws JSONException,
			CommonException {
		GoOracleFindDefinitionOperation op = new GoOracleFindDefinitionOperation("gopath");
		
		try {
			FindDefinitionResult result = op.parseJsonResult(toolOutput);
			assertAreEqual(result, expectedResult);
		} catch(CommonException ce) {
			assertTrue(expectedResult == null);
		}
{'fraction_non_alphanumeric': 0.1550826699326393, 'fraction_numerical': 0.0076546233925290875, 'mean_word_length': 5.72811534500515, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6445774', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1877, 'n_words': 370}