Skip to content

Commit a95b6dd

Browse files
author
Andrei C
committed
Adding builtin support to create CSV files
1 parent f2ec11b commit a95b6dd

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

src/main/java/net/andreinc/mockneat/MockNeat.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ public Countries countries() {
244244
*/
245245
public Currencies currencies() { return this.rCurrencies; }
246246

247+
/**
248+
* <p> Returns a {@code CSVs} object that can be used to generate CSV lines or files.</p>
249+
*
250+
* @return A new CSVs object.
251+
*/
252+
public CSVs csvs() { return new CSVs(this); }
253+
247254
/**
248255
* <p>Returns a {@code CVVS} object that can be used to generate credit card cvv codes.</p>
249256
*
@@ -350,8 +357,6 @@ public <T> Filler<T> filler(Supplier<T> supplier) {
350357
/**
351358
* <p>Returns a {@code Formatter} object than can be used to generate arbitrary patterns based on a given format.</p>
352359
*
353-
* <p>Eg.: m.fmt("#{oneInt} + #{aLetter}").params("oneInt", m.ints(), "aLetter", m.chars().letters()).val()</p>
354-
*
355360
* @param fmt The template of the desired pattern.
356361
* @return A <strong>new</strong> {@code Formatter} object. The {@code Formatter} class implements {@code MockUnitString}.
357362
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package net.andreinc.mockneat.unit.text;
2+
3+
import net.andreinc.mockneat.MockNeat;
4+
import net.andreinc.mockneat.abstraction.*;
5+
import org.apache.commons.text.StringEscapeUtils;
6+
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.function.Supplier;
10+
11+
import static net.andreinc.mockneat.abstraction.MockConstValue.constant;
12+
import static net.andreinc.mockneat.abstraction.MockUnitValue.unit;
13+
import static net.andreinc.mockneat.utils.ValidationUtils.notEmpty;
14+
import static net.andreinc.mockneat.utils.ValidationUtils.notNull;
15+
16+
public class CSVs extends MockUnitBase implements MockUnitString {
17+
18+
public CSVs(MockNeat mockNeat) {
19+
super(mockNeat);
20+
}
21+
22+
private String separator = ",";
23+
private List<MockValue> columns = new LinkedList<>();
24+
25+
public void separator(String separator) {
26+
notEmpty(separator, "separator");
27+
this.separator = separator;
28+
}
29+
30+
/**
31+
* Add a new column to the resulting csv line.
32+
* Internally the supplied mockUnit is transformed to a MockUnitString and afterwards
33+
* the `escapeCsvMethod()` is called.
34+
* @param mockUnit The supplied mockUnit
35+
*/
36+
public CSVs addColumn(MockUnit mockUnit) {
37+
notNull(mockUnit, "mockUnit");
38+
columns.add(unit(mockUnit.mapToString().escapeCsv()));
39+
return this;
40+
}
41+
42+
/**
43+
* Add a new column to resulting CVS line with a constant value.
44+
* The toString() method of the supplied object is invoked.
45+
* @param value The constant value used
46+
*/
47+
public CSVs addColumn(Object value) {
48+
notNull(value, "value");
49+
columns.add(constant(value));
50+
return this;
51+
}
52+
53+
@Override
54+
public Supplier<String> supplier() {
55+
Supplier<String> supplier = () -> {
56+
StringBuilder buff = new StringBuilder();
57+
columns.stream().forEach((v) -> {
58+
if (v instanceof MockConstValue) {
59+
buff.append(StringEscapeUtils.escapeCsv(v.getStr()));
60+
}
61+
else if (v instanceof MockUnitValue) {
62+
buff.append(v.get());
63+
}
64+
buff.append(separator);
65+
});
66+
buff.delete(buff.length() - separator.length(), buff.length());
67+
return buff.toString();
68+
};
69+
return supplier;
70+
}
71+
72+
public void toFile(String filePath, int numberOfLines) {
73+
74+
}
75+
}

src/test/java/net/andreinc/mockneat/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Constants {
3131
public static final int CHARS_CYCLES = 1000;
3232
public static final int COMPOSE_CYCLES = 1000;
3333
public static final int CVVS_CYCLES= 1000;
34+
public static final int CSVS_CYCLES = 1000;
3435
public static final int DEP_CYCLES = 1000;
3536
public static final int CITIES_CYCLES= 1000;
3637
public static final int COUNTRIES_CYCLES = 1000;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package net.andreinc.mockneat.unit.text;
2+
3+
import net.andreinc.mockneat.abstraction.MockUnit;
4+
import net.andreinc.mockneat.abstraction.MockUnitString;
5+
import net.andreinc.mockneat.utils.LoopsUtils;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import static net.andreinc.mockneat.Constants.CSVS_CYCLES;
10+
import static net.andreinc.mockneat.Constants.M;
11+
import static net.andreinc.mockneat.Constants.MOCKS;
12+
13+
public class CSVsTest {
14+
15+
@Test
16+
public void testLine() {
17+
MockUnitString ms1 = M.fromStrings(new String[]{"ABC\","});
18+
String line = M.csvs()
19+
.addColumn(ms1)
20+
.addColumn("A,a")
21+
.val();
22+
Assert.assertTrue(line.equals("\"ABC\"\",\",\"A,a\""));
23+
}
24+
25+
@Test
26+
public void testLineLoop() {
27+
MockUnit<Boolean> torf = M.bools();
28+
LoopsUtils.loop(
29+
CSVS_CYCLES,
30+
MOCKS,
31+
m -> {
32+
return m.csvs()
33+
.addColumn("A")
34+
.addColumn(torf)
35+
.addColumn(m.names())
36+
.val();
37+
},
38+
s -> {
39+
String[] split = s.split(",");
40+
Assert.assertTrue(split.length == 3);
41+
Assert.assertTrue(split[0].equals("A"));
42+
Assert.assertTrue(split[1].equals("true") || split[1].equals("false"));
43+
}
44+
45+
);
46+
}
47+
48+
@Test(expected = NullPointerException.class)
49+
public void testLineNullMockUnit() throws Exception {
50+
M.csvs().addColumn(null).val();
51+
}
52+
}

0 commit comments

Comments
 (0)