public void TestNested() { try { IScope scope = new ScopeChain(GetBootstrapScope()); string[] lines = { ":result v= 0", "if flag1?", " if flag2?", " :result = 1", " else", " :result = 2", "else", " if flag3?", " :result = 3", " else", " :result = 4", }; { // nested if block is run scope.SetValue("flag1?", ValueBool.True); scope.SetValue("flag2?", ValueBool.True); LineConsumer requestor = new LineConsumer(lines); EvalLines.Do(requestor, scope); Assert.AreEqual(1, scope.GetValue(new Token("result")).AsInt); } { // nested else is run scope.SetValue("flag1?", ValueBool.True); scope.SetValue("flag2?", ValueBool.False); LineConsumer requestor = new LineConsumer(lines); EvalLines.Do(requestor, scope); Assert.AreEqual(2, scope.GetValue(new Token("result")).AsInt); } { // top level else block is run, nested if scope.SetValue("flag1?", ValueBool.False); scope.SetValue("flag3?", ValueBool.True); LineConsumer requestor = new LineConsumer(lines); EvalLines.Do(requestor, scope); Assert.AreEqual(3, scope.GetValue(new Token("result")).AsInt); } { // top level else block is run, nested else scope.SetValue("flag1?", ValueBool.False); scope.SetValue("flag3?", ValueBool.False); LineConsumer requestor = new LineConsumer(lines); EvalLines.Do(requestor, scope); Assert.AreEqual(4, scope.GetValue(new Token("result")).AsInt); } } catch (Loki3Exception e) { Assert.Fail(e.ToString()); } }
public void TestParamMetadata2() { try { IScope scope = new ScopeChain(GetBootstrapScope()); { ScopeChain nested = new ScopeChain(scope); Value value = TestSupport.ToValue("{ :a :a :remainder ( :remainder ... ) } v= { :a 4 :b 5 :c 6 }", nested); // value should be { :a 4 :b 5 :c 6 } Assert.AreEqual(3, value.AsMap.Count); // nested should now contain "a" and "remainder" Assert.AreEqual(4, nested.GetValue(new Token("a")).AsInt); Map rest = nested.GetValue(new Token("remainder")).AsMap; Assert.AreEqual(2, rest.Count); Assert.AreEqual(5, rest["b"].AsInt); Assert.AreEqual(6, rest["c"].AsInt); } { ScopeChain nested = new ScopeChain(scope); Value value = TestSupport.ToValue("[ ->a ( ->remainder ... ) ] v= { :a 4 :b 5 :c 6 }", nested); // value should be { :a 4 :b 5 :c 6 } Assert.AreEqual(3, value.AsMap.Count); // nested should now contain "a" and "remainder" Assert.AreEqual(4, nested.GetValue(new Token("a")).AsInt); Map rest = nested.GetValue(new Token("remainder")).AsMap; Assert.AreEqual(2, rest.Count); Assert.AreEqual(5, rest["b"].AsInt); Assert.AreEqual(6, rest["c"].AsInt); } { ScopeChain nested = new ScopeChain(scope); Value value = TestSupport.ToValue("[ ->a ( ->remainder ... ) ] v= [ 7 8 9 ]", nested); // value should be [ 7 8 9 ] Assert.AreEqual(3, value.AsArray.Count); // nested should now contain "a" and "remainder" Assert.AreEqual(7, nested.GetValue(new Token("a")).AsInt); System.Collections.Generic.List<Value> rest = nested.GetValue(new Token("remainder")).AsArray; Assert.AreEqual(2, rest.Count); Assert.AreEqual(8, rest[0].AsInt); Assert.AreEqual(9, rest[1].AsInt); } } catch (Loki3Exception e) { Assert.Fail(e.ToString()); } }
public void TestSetValue() { IScope scope = CreateValueScope(); { Value value = TestSupport.ToValue("l3.setValue { :key :a :value 5 }", scope); Assert.AreEqual(5, value.AsInt); Value fetch = scope.GetValue(new Token("a")); Assert.AreEqual(5, fetch.AsInt); } { // change a Value value = TestSupport.ToValue("l3.setValue { :key :a :value 7.5 }", scope); Assert.AreEqual(7.5, value.AsFloat); Value fetch = scope.GetValue(new Token("a")); Assert.AreEqual(7.5, fetch.AsFloat); } { // set an array Value value = TestSupport.ToValue("l3.setValue { :key :key :value [ a 2 false ] }", scope); List<Value> array = value.AsArray; Assert.AreEqual(3, array.Count); Assert.AreEqual(7.5, array[0].AsFloat); Assert.AreEqual(2, array[1].AsInt); Assert.AreEqual(false, array[2].AsBool); Value fetch = scope.GetValue(new Token("key")); List<Value> fetchArray = fetch.AsArray; Assert.AreEqual(3, fetchArray.Count); Assert.AreEqual(7.5, fetchArray[0].AsFloat); Assert.AreEqual(2, fetchArray[1].AsInt); Assert.AreEqual(false, fetchArray[2].AsBool); } // create a nested scope IScope nested = new ScopeChain(scope); { // explicitly say create-if-needed TestSupport.ToValue("l3.setValue { :key :created :value 3 :create? true }", nested); Value fetch = nested.GetValue(new Token("created")); Assert.AreEqual(3, fetch.AsInt); } { // asking to reuse non-existent var throws bool bThrew = false; try { Value value = TestSupport.ToValue("l3.setValue { :key :doesnt-exist :value 3 :create? false }", nested); } catch (Loki3Exception e) { bThrew = true; Assert.AreEqual("doesnt-exist", e.Errors[Loki3Exception.keyBadToken].AsString); } Assert.IsTrue(bThrew); } { // set var on parent scope & reuse on nested scope TestSupport.ToValue("l3.setValue { :key :on-parent :value 5 :create? true }", scope); Assert.AreEqual(5, scope.AsMap["on-parent"].AsInt); TestSupport.ToValue("l3.setValue { :key :on-parent :value 4 :create? false }", nested); Assert.AreEqual(4, scope.AsMap["on-parent"].AsInt); Assert.AreNotEqual(null, nested.Exists("on-parent")); Assert.IsFalse(nested.AsMap.ContainsKey("on-parent")); } { // set a value on a map Map map = new Map(); map["one"] = new ValueInt(1); map["two"] = new ValueInt(2); ValueMap valueMap = new ValueMap(map); scope.SetValue("mymap", valueMap); Value value = TestSupport.ToValue("l3.setValue { :key :one :value 3 :map mymap }", scope); Assert.AreEqual(3, value.AsInt); Assert.AreEqual(3, map["one"].AsInt); } { // pattern matching: extract values out of an array Value value = TestSupport.ToValue("l3.setValue { :key [ :first :second :third ] :value [ 11 22 33 ] :create? true }", scope); Assert.AreEqual(11, scope.AsMap["first"].AsInt); Assert.AreEqual(22, scope.AsMap["second"].AsInt); Assert.AreEqual(33, scope.AsMap["third"].AsInt); } { // special case: initialize every token in array to nil Value value = TestSupport.ToValue("l3.setValue { :key [ :first :second :third ] :value nil :create? true }", scope); Assert.IsTrue(scope.AsMap["first"].IsNil); Assert.IsTrue(scope.AsMap["second"].IsNil); Assert.IsTrue(scope.AsMap["third"].IsNil); } { // if initOnly?, don't overwrite existing values Value value = TestSupport.ToValue("l3.setValue { :key [ :aa :bb ] :value [ 2 3 ] :create? true :initOnly? true }", scope); Assert.AreEqual(2, scope.AsMap["aa"].AsInt); Assert.AreEqual(3, scope.AsMap["bb"].AsInt); value = TestSupport.ToValue("l3.setValue { :key [ :aa :bb ] :value [ 4 5 ] :create? true :initOnly? true }", scope); Assert.AreEqual(2, scope.AsMap["aa"].AsInt); Assert.AreEqual(3, scope.AsMap["bb"].AsInt); value = TestSupport.ToValue("l3.setValue { :key [ :bb :cc ] :value nil :create? true :initOnly? true }", scope); Assert.AreEqual(3, scope.AsMap["bb"].AsInt); Assert.IsTrue(scope.AsMap["cc"].IsNil); } { // if returnSuccess?, return value tells whether a value was set Value value = TestSupport.ToValue("l3.setValue { :key [ :x :y ] :value [ 2 3 ] :returnSuccess? true }", scope); Assert.IsTrue(value.AsBool); value = TestSupport.ToValue("l3.setValue { :key [ :x :y :z ] :value [ 2 3 ] :returnSuccess? true }", scope); Assert.IsFalse(value.AsBool); } }