summaryrefslogtreecommitdiff
path: root/src/test/java/bjc/replpair/ReplPairTest.java
blob: ec7d8a5a71558148977a6b5fa71cccd9f0d12567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package bjc.replpair;

import java.io.FileInputStream;
import java.io.IOException;

import java.util.List;
import java.util.Scanner;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * Unit test for ReplPair.
 *
 * @author Ben Culkin
 */
public class ReplPairTest {
	@Test
	public void testLoadFile() {
		List<ReplPair> lrp = null;
		String fName = "data/test/test1.rp";

		try (FileInputStream fis = new FileInputStream(fName); Scanner scn = new Scanner(fis)) {
			lrp = ReplPair.readList(scn);

			assertTrue(lrp.size() == 0);
		} catch (IOException ioex) {
			assertTrue(false);
		}
	}

	@Test
	public void testSingleReplace() {
		assertMultiReplace("data/test/test2.rp", "test2", "test1", "test2", "test2");
	}

	@Test
	public void testMultiReplace() {
		assertMultiReplace("data/test/test3.rp", "A B C", "a b c", "A A B", "a a b", "AAB", "aab");
	}

	@Test
	public void testReplaceOrder() {
		assertMultiReplace("data/test/test4.rp", "a", "a", "d", "ab");
	}

	@Test
	public void testReplaceExpOrder() {
		assertMultiReplace("data/test/test5.rp", "a", "a", "aa", "ab");
	}

	@Test
	public void testStaging() {
		assertMultiReplace(true, "data/test/test6.rp", "c", "a", "y2", "x");
	}

	private void assertMultiReplace(String fle, String... inps) {
		assertMultiReplace(false, fle, inps);
	}

	private void assertMultiReplace(boolean logRep, String fle, String... inps) {
		if (inps.length < 2) throw new IllegalArgumentException("ERROR: Must provide at least two strings to assertMultiReplace");
		if (inps.length % 2 != 0) throw new IllegalArgumentException("ERROR: Odd number of strings passed to assertMultiReplace");

		List<ReplPair> lrp = null;

		try (FileInputStream fis = new FileInputStream(fle); Scanner scn = new Scanner(fis)) {
			lrp = ReplPair.readList(scn);
		} catch (Exception ex) {
			System.err.println("EXCEPTION");
			ex.printStackTrace();

			assertTrue(false);

			return;
		}

		for (int i = 0; i < inps.length; i += 2) {
			String right = inps[i];
			String inp   = inps[i + 1];

			assertReplacesTo(logRep, right, lrp, inp);
		}
	}

	private void assertReplacesFrom(String right, String inp, String fle) {
		assertMultiReplace(fle, right, inp);
	}

	private void assertReplacesTo(String right, List<ReplPair> rps, String inp) {
		assertReplacesTo(false, right, rps, inp);
	}

	private void assertReplacesTo(boolean logRep, String right, List<ReplPair> rps, String inp) {
		if (logRep) System.err.printf("\t[LOG] Checking '%s' -> '%s'\n", inp, right);

		String tmp = inp;

		for (ReplPair rp : rps) {
			String oldTmp = tmp;

			tmp = rp.apply(tmp);

			if (logRep) System.err.printf("\t[LOG] '%s' -> '%s'\t%s\n", oldTmp, tmp, rp);
		}

		assertEquals(right, tmp);
	}
}