Skip to content

Commit

Permalink
Added an output.txt for end-to-end Dictionary tests (#512)
Browse files Browse the repository at this point in the history
Required fixing up loops or lookups in a few languages:
* JavaScript and TypeScript were using `.hasOwnProperty(` instead of `{}.hasOwnProperty.call(`
* Python was using `iteritems()` instead of `items()`
* TypeScript was printing types in `for` loops
  • Loading branch information
Josh Goldberg authored Nov 2, 2018
1 parent b1eb93e commit e0950d1
Show file tree
Hide file tree
Showing 38 changed files with 330 additions and 141 deletions.
16 changes: 12 additions & 4 deletions src/Rendering/Commands/ForEachKeyStartCommand.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Import } from "../Languages/Imports/Import";
import { LineResults } from "../LineResults";
import { CommandNames } from "../Names/CommandNames";
import { Command } from "./Command";
Expand Down Expand Up @@ -52,14 +53,21 @@ export class ForEachKeyStartCommand extends Command {
public renderForEachAsLoop(parameters: string[]): LineResults {
let line: string = this.language.syntax.loops.foreach;
let output: CommandResult[];
const imports: Import[] = [];

line += this.language.syntax.conditionals.startLeft;

if (this.language.syntax.variables.declarationRequired) {
const variableInline = this.context.convertParsed([CommandNames.VariableInline, parameters[2], parameters[3]]);

line += this.language.syntax.variables.declaration;
line += variableInline.commandResults[0].text;

if (this.language.syntax.loops.forEachPairsTypedPair) {
const variableInline = this.context.convertParsed([CommandNames.VariableInline, parameters[2], parameters[3]]);

line += variableInline.commandResults[0].text;
imports.push(...variableInline.addedImports);
} else {
line += parameters[2];
}
} else {
line += parameters[2];
}
Expand All @@ -72,7 +80,7 @@ export class ForEachKeyStartCommand extends Command {
output = [new CommandResult(line, 0)];
this.addLineEnder(output, this.language.syntax.conditionals.startRight, 1);

return new LineResults(output);
return new LineResults(output).withImports(imports);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Rendering/Commands/ForEachPairStartCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ export class ForEachPairStartCommand extends Command {
}

line += this.language.syntax.variables.declaration;
line += this.context.convertParsed([CommandNames.VariableInline, iteratorName, typeName]).commandResults[0].text;

if (this.language.syntax.loops.forEachPairsTypedPair) {
const typedLine = this.context.convertParsed([CommandNames.VariableInline, iteratorName, typeName]);
line += typedLine.commandResults[0].text;
imports.push(...typedLine.addedImports);
} else {
line += iteratorName;
}
} else {
line += parameters[3];

Expand Down
1 change: 1 addition & 0 deletions src/Rendering/Languages/CSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ export class CSharp extends Language {
loops.forEachPairsPairClass = "KeyValuePair";
loops.forEachPairsRetrieveKey = ".Key";
loops.forEachPairsRetrieveValue = ".Value";
loops.forEachPairsTypedPair = true;
loops.forEachRight = "";

loops.forEachStartLeft = "foreach";
Expand Down
1 change: 1 addition & 0 deletions src/Rendering/Languages/Java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ export class Java extends Language {
loops.forEachPairsPairClass = "Map.Entry";
loops.forEachPairsRetrieveKey = ".getKey()";
loops.forEachPairsRetrieveValue = ".getValue()";
loops.forEachPairsTypedPair = true;
loops.forEachRight = "";

loops.forEachStartLeft = "for";
Expand Down
2 changes: 1 addition & 1 deletion src/Rendering/Languages/JavaScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class JavaScript extends Language {
*/
protected generateDictionarySyntax(dictionaries: DictionarySyntax): void {
dictionaries.className = "Object";
dictionaries.containsKey = new NativeCallSyntax("hasOwnProperty", NativeCallScope.Member, NativeCallType.Function);
dictionaries.containsKey = new NativeCallSyntax("{}.hasOwnProperty.call", NativeCallScope.Static, NativeCallType.Function);
dictionaries.initializeAsLiteral = "{}";
dictionaries.keys = new NativeCallSyntax("Object.keys", NativeCallScope.Static, NativeCallType.Function);
dictionaries.initializeEnd = "}";
Expand Down
5 changes: 5 additions & 0 deletions src/Rendering/Languages/Properties/Syntax/LoopSyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export class LoopSyntax {
*/
public forEachPairsRetrieveValue: string;

/**
* Whether pairs should include explicit types.
*/
public forEachPairsTypedPair: boolean;

/**
* How to end a foreach loop's initial line.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Rendering/Languages/Python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class Python extends Language {

loops.forEachEnd = "\0";
loops.forEachGetKeys = "";
loops.forEachGetPairs = ".iteritems()";
loops.forEachGetPairs = ".items()";
loops.forEachKeyEnd = "\0";
loops.forEachPairEnd = "\0";
loops.forEachPairsAsPair = true;
Expand Down
4 changes: 2 additions & 2 deletions src/Rendering/Languages/Ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ export class Ruby extends Language {
* @param dictionaries The property container for metadata on dictionaries.
*/
protected generateDictionarySyntax(dictionaries: DictionarySyntax): void {
dictionaries.containsKey = new NativeCallSyntax(" in ", NativeCallScope.Operator, NativeCallType.FloatingLeft);
dictionaries.containsKey = new NativeCallSyntax("key?", NativeCallScope.Member, NativeCallType.Function);
dictionaries.initializeAsLiteral = "{}";
dictionaries.initializeEnd = "}";
dictionaries.initializePairComma = ",";
dictionaries.initializePairLeft = "";
dictionaries.initializePairMiddle = ": ";
dictionaries.initializePairMiddle = " => ";
dictionaries.initializePairRight = "";
dictionaries.initializeStart = "{";

Expand Down
2 changes: 1 addition & 1 deletion src/Rendering/Languages/TypeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export class TypeScript extends Language {
*/
protected generateDictionarySyntax(dictionaries: DictionarySyntax): void {
dictionaries.className = "Object";
dictionaries.containsKey = new NativeCallSyntax("hasOwnProperty", NativeCallScope.Member, NativeCallType.Function);
dictionaries.containsKey = new NativeCallSyntax("{}.hasOwnProperty.call", NativeCallScope.Static, NativeCallType.Function);
dictionaries.keys = new NativeCallSyntax("Object.keys", NativeCallScope.Static, NativeCallType.Function);
dictionaries.initializeAsLiteral = "{}";
dictionaries.initializeEnd = "}";
Expand Down
52 changes: 52 additions & 0 deletions test/end-to-end/Dictionaries/index.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
using System;
using System.Collections.Generic;

namespace Dictionaries
{
class Index
{
public static void Main()
{
// Types
Dictionary<string, int> foo = new Dictionary<string, int>();
Dictionary<string, Dictionary<string, int>> bar = new Dictionary<string, Dictionary<string, int>>();

// Indices
foo["baz"] = 7;
int qux = foo["baz"];
Console.WriteLine(string.Format("baz is {0}", foo["baz"]));
Console.WriteLine(string.Format("qux is {0}", qux));

// Initialization
Dictionary<string, int> aaa = new Dictionary<string, int>
{
{ "bbb", 1 },
{ "ccc", 2 },
{ "ddd", 3 }
};

// Contains Key
bool containsFalse = aaa.ContainsKey("aaa");

if (containsFalse)
{
Console.WriteLine("wrong");
}

if (aaa.ContainsKey("bbb"))
{
Console.WriteLine("contains bbb");
}

// Pair Iteration
foreach (KeyValuePair<string, int> pair in aaa)
{
string key = pair.Key;
int value = pair.Value;
Console.WriteLine(string.Format("{0} has {1}", key, value));
}
}
}
}
//
6 changes: 6 additions & 0 deletions test/end-to-end/Dictionaries/index.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>
40 changes: 40 additions & 0 deletions test/end-to-end/Dictionaries/index.gls
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-
file start : Dictionaries index
main context start
main start
comment line : Types
variable : foo { dictionary type : string int } { dictionary initialize : string int }
variable : bar { dictionary type : string { dictionary type : string int } } { dictionary initialize : string { dictionary type : string int } }

comment line : Indices
operation : { dictionary index : foo "baz" } equals 7
variable : qux int { dictionary index : foo "baz" }
print : { string format : ("baz is {0}") { dictionary index : foo "baz" } int }
print : { string format : ("qux is {0}") qux int }

comment line : Initialization
variable start : aaa { dictionary type : string int } { dictionary initialize start : string int }
dictionary pair : "bbb" 1 ,
dictionary pair : "ccc" 2 ,
dictionary pair : "ddd" 3
dictionary initialize end

comment line : Contains Key
variable : containsFalse boolean { dictionary contains key : aaa "aaa" }

if start : containsFalse
print : "wrong"
if end

if start : { dictionary contains key : aaa "bbb" }
print : ("contains bbb")
if end

comment line : Pair Iteration
for each pair start : aaa pair key string value int
print : { string format : ("{0} has {1}") key string value int }
for each pair end
main end
main context end
file end
-
44 changes: 44 additions & 0 deletions test/end-to-end/Dictionaries/index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
package dictionaries;

import java.util.HashMap;

class Index {
public static void main(String[] args) {
// Types
HashMap<String, int> foo = new HashMap<String, int>();
HashMap<String, HashMap<String, int>> bar = new HashMap<String, HashMap<String, int>>();

// Indices
foo["baz"] = 7;
int qux = foo["baz"];
System.out.println(String.format("baz is %0$d", foo["baz"]));
System.out.println(String.format("qux is %0$d", qux));

// Initialization
HashMap<String, int> aaa = new HashMap<String, int>() {{
put("bbb", 1);
put("ccc", 2);
put("ddd", 3);
}};

// Contains Key
boolean containsFalse = aaa.containsKey("aaa");

if (containsFalse) {
System.out.println("wrong");
}

if (aaa.containsKey("bbb")) {
System.out.println("contains bbb");
}

// Pair Iteration
for (Map.Entry<String, int> pair : aaa.entrySet()) {
String key = pair.getKey();
int value = pair.getValue();
System.out.println(String.format("%0$s has %1$d", key, value));
}
}
}
//
35 changes: 35 additions & 0 deletions test/end-to-end/Dictionaries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Types
let foo = {};
let bar = {};

// Indices
foo["baz"] = 7;
let qux = foo["baz"];
console.log(`baz is ${foo["baz"]}`);
console.log(`qux is ${qux}`);

// Initialization
let aaa = {
"bbb": 1,
"ccc": 2,
"ddd": 3
};

// Contains Key
let containsFalse = {}.hasOwnProperty.call(aaa, "aaa");

if (containsFalse) {
console.log("wrong");
}

if ({}.hasOwnProperty.call(aaa, "bbb")) {
console.log("contains bbb");
}

// Pair Iteration
for (let key in aaa) {
let value = aaa[key];
console.log(`${key} has ${value}`);
}
//
32 changes: 32 additions & 0 deletions test/end-to-end/Dictionaries/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
if __name__ == "__main__":
# Types
foo = {}
bar = {}

# Indices
foo["baz"] = 7
qux = foo["baz"]
print("baz is {0}".format(foo["baz"]))
print("qux is {0}".format(qux))

# Initialization
aaa = {
"bbb": 1,
"ccc": 2,
"ddd": 3
}

# Contains Key
containsFalse = "aaa" in aaa

if containsFalse:
print("wrong")

if "bbb" in aaa:
print("contains bbb")

# Pair Iteration
for key, value in aaa.items():
print("{0} has {1}".format(key, value))
#
34 changes: 34 additions & 0 deletions test/end-to-end/Dictionaries/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Types
foo = {}
bar = {}

# Indices
foo["baz"] = 7
qux = foo["baz"]
puts "baz is %d" % [foo["baz"]]
puts "qux is %d" % [qux]

# Initialization
aaa = {
"bbb" => 1,
"ccc" => 2,
"ddd" => 3
}

# Contains Key
containsFalse = aaa.key?("aaa")

if containsFalse
puts "wrong"
end

if aaa.key?("bbb")
puts "contains bbb"
end

# Pair Iteration
aaa.each { |key, value|
puts "%s has %d" % [key, value]
}
#
Loading

0 comments on commit e0950d1

Please sign in to comment.