-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding builtin support to create CSV files
- Loading branch information
Andrei C
committed
Jul 5, 2018
1 parent
f2ec11b
commit a95b6dd
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package net.andreinc.mockneat.unit.text; | ||
|
||
import net.andreinc.mockneat.MockNeat; | ||
import net.andreinc.mockneat.abstraction.*; | ||
import org.apache.commons.text.StringEscapeUtils; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import static net.andreinc.mockneat.abstraction.MockConstValue.constant; | ||
import static net.andreinc.mockneat.abstraction.MockUnitValue.unit; | ||
import static net.andreinc.mockneat.utils.ValidationUtils.notEmpty; | ||
import static net.andreinc.mockneat.utils.ValidationUtils.notNull; | ||
|
||
public class CSVs extends MockUnitBase implements MockUnitString { | ||
|
||
public CSVs(MockNeat mockNeat) { | ||
super(mockNeat); | ||
} | ||
|
||
private String separator = ","; | ||
private List<MockValue> columns = new LinkedList<>(); | ||
|
||
public void separator(String separator) { | ||
notEmpty(separator, "separator"); | ||
this.separator = separator; | ||
} | ||
|
||
/** | ||
* Add a new column to the resulting csv line. | ||
* Internally the supplied mockUnit is transformed to a MockUnitString and afterwards | ||
* the `escapeCsvMethod()` is called. | ||
* @param mockUnit The supplied mockUnit | ||
*/ | ||
public CSVs addColumn(MockUnit mockUnit) { | ||
notNull(mockUnit, "mockUnit"); | ||
columns.add(unit(mockUnit.mapToString().escapeCsv())); | ||
return this; | ||
} | ||
|
||
/** | ||
* Add a new column to resulting CVS line with a constant value. | ||
* The toString() method of the supplied object is invoked. | ||
* @param value The constant value used | ||
*/ | ||
public CSVs addColumn(Object value) { | ||
notNull(value, "value"); | ||
columns.add(constant(value)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Supplier<String> supplier() { | ||
Supplier<String> supplier = () -> { | ||
StringBuilder buff = new StringBuilder(); | ||
columns.stream().forEach((v) -> { | ||
if (v instanceof MockConstValue) { | ||
buff.append(StringEscapeUtils.escapeCsv(v.getStr())); | ||
} | ||
else if (v instanceof MockUnitValue) { | ||
buff.append(v.get()); | ||
} | ||
buff.append(separator); | ||
}); | ||
buff.delete(buff.length() - separator.length(), buff.length()); | ||
return buff.toString(); | ||
}; | ||
return supplier; | ||
} | ||
|
||
public void toFile(String filePath, int numberOfLines) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/net/andreinc/mockneat/unit/text/CSVsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package net.andreinc.mockneat.unit.text; | ||
|
||
import net.andreinc.mockneat.abstraction.MockUnit; | ||
import net.andreinc.mockneat.abstraction.MockUnitString; | ||
import net.andreinc.mockneat.utils.LoopsUtils; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static net.andreinc.mockneat.Constants.CSVS_CYCLES; | ||
import static net.andreinc.mockneat.Constants.M; | ||
import static net.andreinc.mockneat.Constants.MOCKS; | ||
|
||
public class CSVsTest { | ||
|
||
@Test | ||
public void testLine() { | ||
MockUnitString ms1 = M.fromStrings(new String[]{"ABC\","}); | ||
String line = M.csvs() | ||
.addColumn(ms1) | ||
.addColumn("A,a") | ||
.val(); | ||
Assert.assertTrue(line.equals("\"ABC\"\",\",\"A,a\"")); | ||
} | ||
|
||
@Test | ||
public void testLineLoop() { | ||
MockUnit<Boolean> torf = M.bools(); | ||
LoopsUtils.loop( | ||
CSVS_CYCLES, | ||
MOCKS, | ||
m -> { | ||
return m.csvs() | ||
.addColumn("A") | ||
.addColumn(torf) | ||
.addColumn(m.names()) | ||
.val(); | ||
}, | ||
s -> { | ||
String[] split = s.split(","); | ||
Assert.assertTrue(split.length == 3); | ||
Assert.assertTrue(split[0].equals("A")); | ||
Assert.assertTrue(split[1].equals("true") || split[1].equals("false")); | ||
} | ||
|
||
); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testLineNullMockUnit() throws Exception { | ||
M.csvs().addColumn(null).val(); | ||
} | ||
} |