summaryrefslogtreecommitdiff
path: root/src/test/java/bjc/everge/TestUtils.java
blob: f3898c78aae90fa8e059bbbc8bd6efa6e1653e23 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package bjc.everge;

import bjc.everge.ControlledString.Control;
import bjc.everge.ControlledString.ParseStrings;

import java.io.FileInputStream;

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

import static org.junit.Assert.*;

/**
 * Utility methods for testing.
 *
 * @author Ben Culkin
 */
@SuppressWarnings("javadoc")
public class TestUtils {
	public static void assertThrownMessage(String msg, String fle) {
		assertThrownMessage(false, msg, fle);
	}

	public static void assertThrownMessage(boolean logMsg, String msg, String fle) {
		try (FileInputStream fis = new FileInputStream(fle);
				Scanner scn = new Scanner(fis)) {
			ReplPair.readList(new ArrayList<>(), scn);

			assertTrue(false);
		} catch (BadReplParse rpex) {
			if (logMsg)
				System.err.println(rpex.toPrintString());

			assertEquals(msg, rpex.toPrintString());
		} catch (Exception ex) {
			System.err.println("EXCEPTION");
			ex.printStackTrace();

			assertTrue(false);

			return;
		}
	}

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

	public static 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 (BadReplParse rpex) {
			System.err.println(rpex.toPrintString());

			assertTrue(false);
		} 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);
		}
	}

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

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

	public static 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\t[LOG-STEP] '%s' -> '%s'\t%s\n", oldTmp, tmp, rp);
			}
		}

		assertEquals(right, tmp);
	}

	public static void assertSplitsTo(String inp, String esc, String splat,
			String... right) {
		assertSplitsTo(false, inp, esc, splat, right);
	}

	public static void assertSplitsTo(boolean doLog, String inp, String esc, String splat,
			String... right) {
		try {
			if (doLog)
				StringUtils.isDebug = true;

			String[] lst = StringUtils.escapeSplit(esc, splat, inp);

			if (doLog) {
				System.err.printf("[TRACE] Returned ");

				for (String str : lst) {
					System.err.printf("(%s) ", str);
				}

				System.err.println();
			}

			assertArrayEquals(right, lst);
		} catch (Exception ex) {
			System.err.println("EXCEPTION");
			ex.printStackTrace();
			System.err.println();

			assertTrue(false);
		} finally {
			if (doLog)
				StringUtils.isDebug = false;
		}
	}

	public static void assertIsControl(String inp, String strang, Control... args) {
		assertIsControl(false, inp, strang, args);
	}

	public static void assertIsControl(boolean doLog, String inp, String strang,
			Control... args) {
		ControlledString cs
				= ControlledString.parse(inp, new ParseStrings("//", ";", "/", "|"));

		if (doLog) {
			System.err.printf("[LOG] CS: %s\n", cs);
		}

		assertEquals(strang, cs.strang);

		assertEquals("array length mismatch:", args.length, cs.count());

		for (int i = 0; i < args.length; i++) {
			assertEquals("array value mismatch:", args[i], cs.controls[i]);
		}
	}
	
	public static void assertMatches(String pattern, String actual) {
		assertTrue(actual.matches(pattern));
	}
}