Skip to content

Commit

Permalink
Replaces golden strings in source map tests with a structured represe…
Browse files Browse the repository at this point in the history
…ntation.

This CL introduces a minimal builder for the source map JSON file structure so that tests are less error prone. It also changes the assertions to be about the parsed structure, rather than string, to reduce brittleness.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=232784760
  • Loading branch information
nreid260 authored and tjgq committed Feb 8, 2019
1 parent 00f1ae7 commit 49c66c7
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 204 deletions.
114 changes: 59 additions & 55 deletions test/com/google/debugging/sourcemap/SourceMapConsumerV3Test.java
Expand Up @@ -18,6 +18,9 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import java.util.Map;
Expand All @@ -32,65 +35,66 @@
@RunWith(JUnit4.class)
public final class SourceMapConsumerV3Test {

private static final Gson GSON = new Gson();

@Test
public void testSources() throws Exception {
String sourceMap = "{\n" +
"\"version\":3,\n" +
"\"file\":\"testcode\",\n" +
"\"lineCount\":1,\n" +
"\"mappings\":\"AAAAA,QAASA,UAAS,EAAG;\",\n" +
"\"sources\":[\"testcode\"],\n" +
"\"names\":[\"__BASIC__\"]\n" +
"}\n";

SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
consumer.parse(sourceMap);
consumer.parse(
GSON.toJson(
TestJsonBuilder.create()
.setVersion(3)
.setFile("testcode")
.setLineCount(1)
.setMappings("AAAAA,QAASA,UAAS,EAAG;")
.setSources("testcode")
.setNames("__BASIC__")
.build()));

assertThat(consumer.getOriginalSources()).containsExactly("testcode");
assertThat(consumer.getSourceRoot()).isNull();
}

@Test
public void testMap() throws Exception {
String sourceMap = ""
+ "{"
+ " \"version\": 3,"
+ " \"file\": \"testcode.js\","
+ " \"sections\": ["
+ " {"
+ " \"map\": {"
+ " \"version\": 3,"
+ " \"mappings\": \"AAAAA,QAASA,UAAS,EAAG;\","
+ " \"sources\": [\"testcode.js\"],"
+ " \"names\": [\"foo\"]"
+ " },"
+ " \"offset\": {"
+ " \"line\": 1,"
+ " \"column\": 1"
+ " }"
+ " }"
+ " ]"
+ "}";

SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
consumer.parse(sourceMap);

consumer.parse(
GSON.toJson(
TestJsonBuilder.create()
.setVersion(3)
.setFile("testcode")
.setCustomProperty(
"sections",
ImmutableList.of(
ImmutableMap.builder()
.put(
"map",
TestJsonBuilder.create()
.setVersion(3)
.setMappings("AAAAA,QAASA,UAAS,EAAG;")
.setSources("testcode.js")
.setNames("foo")
.build())
.put("offset", ImmutableMap.of("line", 1, "column", 2))
.build()))
.build()));
}

@Test
public void testSourcesWithRoot() throws Exception {
String sourceMap = "{\n" +
"\"version\":3,\n" +
"\"file\":\"testcode\",\n" +
"\"lineCount\":1,\n" +
"\"sourceRoot\":\"http://server/path/\",\n" +
"\"mappings\":\"AAAAA,QAASA,UAAS,EAAG;\",\n" +
"\"sources\":[\"testcode\"],\n" +
"\"names\":[\"__BASIC__\"]\n" +
"}\n";

SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
consumer.parse(sourceMap);
consumer.parse(
GSON.toJson(
TestJsonBuilder.create()
.setVersion(3)
.setFile("testcode")
.setLineCount(1)
.setMappings("AAAAA,QAASA,UAAS,EAAG;")
.setSourceRoot("http://server/path/")
.setSources("testcode")
.setNames("__BASIC__")
.build()));

//By default sourceRoot is not prepended
assertThat(consumer.getOriginalSources()).containsExactly("testcode");
Expand All @@ -99,25 +103,25 @@ public void testSourcesWithRoot() throws Exception {

@Test
public void testExtensions() throws Exception {
String sourceMap = "{\n" +
"\"version\":3,\n" +
"\"file\":\"testcode\",\n" +
"\"lineCount\":1,\n" +
"\"mappings\":\"AAAAA,QAASA,UAAS,EAAG;\",\n" +
"\"sources\":[\"testcode\"],\n" +
"\"names\":[\"__BASIC__\"],\n" +
"\"x_org_int\":2,\n" +
"\"x_org_array\":[]\n" +
"}\n";

SourceMapConsumerV3 consumer = new SourceMapConsumerV3();
consumer.parse(sourceMap);
consumer.parse(
GSON.toJson(
TestJsonBuilder.create()
.setVersion(3)
.setFile("testcode")
.setLineCount(1)
.setMappings("AAAAA,QAASA,UAAS,EAAG;")
.setSources("testcode")
.setNames("__BASIC__")
.setCustomProperty("x_org_int", 2)
.setCustomProperty("x_org_array", ImmutableList.of())
.build()));

Map<String, Object> exts = consumer.getExtensions();

assertThat(exts).hasSize(2);
assertThat(exts).doesNotContainKey("org_int");
assertThat(((JsonElement) exts.get("x_org_int")).getAsInt()).isEqualTo(2);
assertThat(((JsonArray) exts.get("x_org_array")).size()).isEqualTo(0);
assertThat((JsonArray) exts.get("x_org_array")).isEmpty();
}
}

0 comments on commit 49c66c7

Please sign in to comment.