Skip to content

Commit

Permalink
Adding builtin support to create CSV files
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei C committed Jul 5, 2018
1 parent f2ec11b commit a95b6dd
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/net/andreinc/mockneat/MockNeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ public Countries countries() {
*/
public Currencies currencies() { return this.rCurrencies; }

/**
* <p> Returns a {@code CSVs} object that can be used to generate CSV lines or files.</p>
*
* @return A new CSVs object.
*/
public CSVs csvs() { return new CSVs(this); }

/**
* <p>Returns a {@code CVVS} object that can be used to generate credit card cvv codes.</p>
*
Expand Down Expand Up @@ -350,8 +357,6 @@ public <T> Filler<T> filler(Supplier<T> supplier) {
/**
* <p>Returns a {@code Formatter} object than can be used to generate arbitrary patterns based on a given format.</p>
*
* <p>Eg.: m.fmt("#{oneInt} + #{aLetter}").params("oneInt", m.ints(), "aLetter", m.chars().letters()).val()</p>
*
* @param fmt The template of the desired pattern.
* @return A <strong>new</strong> {@code Formatter} object. The {@code Formatter} class implements {@code MockUnitString}.
*/
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/net/andreinc/mockneat/unit/text/CSVs.java
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) {

}
}
1 change: 1 addition & 0 deletions src/test/java/net/andreinc/mockneat/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Constants {
public static final int CHARS_CYCLES = 1000;
public static final int COMPOSE_CYCLES = 1000;
public static final int CVVS_CYCLES= 1000;
public static final int CSVS_CYCLES = 1000;
public static final int DEP_CYCLES = 1000;
public static final int CITIES_CYCLES= 1000;
public static final int COUNTRIES_CYCLES = 1000;
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/net/andreinc/mockneat/unit/text/CSVsTest.java
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();
}
}

0 comments on commit a95b6dd

Please sign in to comment.