Skip to content

Commit

Permalink
Merge pull request #58 from exodrifter/refactor_compiler
Browse files Browse the repository at this point in the history
* You can now add the enum argument `cant_skip` to the end of a pause statement
  to ignore advances until the pause ends
* You can now add the enum argument `no_wait` to the end of an add or say
  statement to auto advance the dialog
* Variable substitution with `{` and `}` in strings is now supported
* The compiler no longer uses a tokenizer except when parsing expressions
* Compiler errors are more specific and descriptive
* The `Exodrifter.Rumor.Lang` namespace has been renamed to
  `Exodrifter.Rumor.Language`
* All unit tests and examples are wrapped in a `UNITY_EDITOR` ifdef to make
  it easier to use this repository as a submodule in non-unity project
* Bindings are no longer stored in `Rumor`; instead, it is now stored in
  `Bindings`
* `Rumor.Run` has been renamed to `Rumor.Start` in order to match the language
  used in C# `Thread.Start` and Unity's `StartCoroutine` method to improve
  consistency
  • Loading branch information
exodrifter authored Jan 15, 2018
2 parents 8f79486 + c7fa3d1 commit 445a11e
Show file tree
Hide file tree
Showing 118 changed files with 5,455 additions and 3,030 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
* You can now add the enum argument `cant_skip` to the end of a pause statement
to ignore advances until the pause ends
* You can now add the enum argument `no_wait` to the end of an add or say
statement to auto advance the dialog
* Variable substitution with `{` and `}` in strings is now supported

### Changed
* The compiler no longer uses a tokenizer except when parsing expressions
* Compiler errors are more specific and descriptive
* The `Exodrifter.Rumor.Lang` namespace has been renamed to
`Exodrifter.Rumor.Language`
* All unit tests and examples are wrapped in a `UNITY_EDITOR` ifdef to make
it easier to use this repository as a submodule in non-unity project
* Bindings are no longer stored in `Rumor`; instead, it is now stored in
`Bindings`
* `Rumor.Run` has been renamed to `Rumor.Start` in order to match the language
used in C# `Thread.Start` and Unity's `StartCoroutine` method to improve
consistency


## [1.1.2] - 2017-12-23

### Changed
Expand Down
115 changes: 115 additions & 0 deletions Editor/Tests/Engine/BindingsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#if UNITY_EDITOR

using Exodrifter.Rumor.Engine;
using NUnit.Framework;
using System;

namespace Exodrifter.Rumor.Test.Engine
{
/// <summary>
/// Ensure function calls work as expected
/// </summary>
public class BindingsTest
{
/// <summary>
/// Check if <see cref="Action"/> bindings work.
/// </summary>
[Test]
public void Action()
{
var bindings = new Bindings();

bool a0 = false;
bindings.Bind("a0", () => a0 = true);
bindings.CallBinding("a0");
Assert.IsTrue(a0);

int a1 = 0;
bindings.Bind("a1", (int p1) => a1 = p1);
bindings.CallBinding("a1", 1);
Assert.AreEqual(1, a1);

int a2 = 0;
bindings.Bind("a2", (int p1, int p2) => { a1 = p1; a2 = p2; });
bindings.CallBinding("a2", 2, 3);
Assert.AreEqual(2, a1);
Assert.AreEqual(3, a2);

int a3 = 0;
bindings.Bind("a3", (int p1, int p2, int p3) => {
a1 = p1; a2 = p2; a3 = p3;
});
bindings.CallBinding("a3", 3, 4, 5);
Assert.AreEqual(3, a1);
Assert.AreEqual(4, a2);
Assert.AreEqual(5, a3);

int a4 = 0;
bindings.Bind("a4", (int p1, int p2, int p3, int p4) => {
a1 = p1; a2 = p2; a3 = p3; a4 = p4;
});
bindings.CallBinding("a4", 4, 5, 6, 7);
Assert.AreEqual(4, a1);
Assert.AreEqual(5, a2);
Assert.AreEqual(6, a3);
Assert.AreEqual(7, a4);
}

/// <summary>
/// Check if <see cref="Func"/> bindings work.
/// </summary>
[Test]
public void Func()
{
var bindings = new Bindings();

bindings.Bind("a0", () => { return true; });
var a0 = bindings.CallBinding("a0");
Assert.IsTrue((bool)a0);

bindings.Bind("a1", (int p1) => { return p1; });
var a1 = bindings.CallBinding("a1", 1);
Assert.AreEqual(1, a1);

bindings.Bind("a2", (int p1, int p2) => { return p1 + p2; });
var a2 = bindings.CallBinding("a2", 1, 2);
Assert.AreEqual(3, a2);

bindings.Bind("a3", (int p1, int p2, int p3) => {
return p1 + p2 + p3;
});
var a3 = bindings.CallBinding("a3", 1, 2, 3);
Assert.AreEqual(6, a3);

bindings.Bind("a4", (int p1, int p2, int p3, int p4) => {
return p1 + p2 + p3 + p4;
});
var a4 = bindings.CallBinding("a4", 1, 2, 3, 5);
Assert.AreEqual(11, a4);
}

[Test]
public void CallUnmappedBinding()
{
var bindings = new Bindings();
bindings.Bind("foo", () => { return true; });

Assert.Throws<InvalidOperationException>(
() => bindings.CallBinding("bar")
);
}

[Test]
public void BindAgain()
{
var bindings = new Bindings();
bindings.Bind("foo", () => { return true; });

Assert.Throws<InvalidOperationException>(
() => bindings.Bind("foo", () => { return false; })
);
}
}
}

#endif
File renamed without changes.
6 changes: 5 additions & 1 deletion Editor/Tests/Engine/RumorStateTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Exodrifter.Rumor.Engine;
#if UNITY_EDITOR

using Exodrifter.Rumor.Engine;
using NUnit.Framework;

namespace Exodrifter.Rumor.Test.Engine
Expand Down Expand Up @@ -265,3 +267,5 @@ public void ClearChoices()
}
}
}

#endif
14 changes: 9 additions & 5 deletions Editor/Tests/Engine/RumorTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Exodrifter.Rumor.Nodes;
#if UNITY_EDITOR

using Exodrifter.Rumor.Nodes;
using NUnit.Framework;
using System.Collections.Generic;

Expand All @@ -17,7 +19,7 @@ public void EmptyRumor()
{
var rumor = new Rumor.Engine.Rumor(new List<Node>());

var iter = rumor.Run();
var iter = rumor.Start();
iter.MoveNext();

Assert.True(rumor.Started);
Expand All @@ -38,7 +40,7 @@ public void SimpleRumor()
new Say("b"),
});

var iter = rumor.Run();
var iter = rumor.Start();

Assert.False(rumor.Started);
Assert.False(rumor.Finished);
Expand Down Expand Up @@ -94,7 +96,7 @@ public void RumorJumpWithoutStart()
Assert.IsFalse(rumor.Running);
Assert.IsFalse(rumor.Finished);

var yield = rumor.Run();
var yield = rumor.Start();
yield.MoveNext();
Assert.AreEqual("b", (rumor.Current as Say).EvaluateText(rumor));
Assert.IsTrue(rumor.Started);
Expand All @@ -120,7 +122,7 @@ public void RumorCallWithoutStart()
Assert.IsFalse(rumor.Running);
Assert.IsFalse(rumor.Finished);

var yield = rumor.Run();
var yield = rumor.Start();
yield.MoveNext();
Assert.AreEqual("b", (rumor.Current as Say).EvaluateText(rumor));
Assert.IsTrue(rumor.Started);
Expand All @@ -129,3 +131,5 @@ public void RumorCallWithoutStart()
}
}
}

#endif
132 changes: 132 additions & 0 deletions Editor/Tests/Engine/ScopeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#if UNITY_EDITOR

using Exodrifter.Rumor.Engine;
using NUnit.Framework;

namespace Exodrifter.Rumor.Test.Engine
{
/// <summary>
/// Ensure Scope objects operate as expected.
/// </summary>
public class ScopeTest
{
#region Events

[Test]
public void OnClearTest()
{
var success = false;
var scope = new Scope();
scope.OnSet += (name, value) => Assert.Fail();
scope.OnClear += () =>
{
success = true;
};

scope.ClearVars();
Assert.IsTrue(success);
}

[Test]
public void OnSetTest()
{
var scope = new Scope();
scope.OnClear += () => Assert.Fail();
scope.OnSet += (name, value) =>
{
Assert.AreEqual("foo", name);
Assert.IsTrue(value.IsBool());
Assert.IsTrue(value.AsBool());
};

scope.SetVar("foo", true);
}

#endregion

#region Setters

[Test]
public void SetBoolTest()
{
var scope = new Scope();

scope.SetVar("foo", true);
Assert.IsTrue(scope.GetVar("foo").IsBool());
Assert.IsTrue(scope.GetVar("foo").AsBool());

scope.SetVar("foo", false);
Assert.IsTrue(scope.GetVar("foo").IsBool());
Assert.IsFalse(scope.GetVar("foo").AsBool());
}

[Test]
public void SetIntTest()
{
var scope = new Scope();

scope.SetVar("foo", 1);
Assert.IsTrue(scope.GetVar("foo").IsInt());
Assert.AreEqual(1, scope.GetVar("foo").AsInt());

scope.SetVar("foo", -100);
Assert.IsTrue(scope.GetVar("foo").IsInt());
Assert.AreEqual(-100, scope.GetVar("foo").AsInt());
}

[Test]
public void SetFloatTest()
{
var scope = new Scope();

scope.SetVar("foo", 1.0f);
Assert.IsTrue(scope.GetVar("foo").IsFloat());
Assert.AreEqual(1.0f, scope.GetVar("foo").AsFloat());

scope.SetVar("foo", -100.0f);
Assert.IsTrue(scope.GetVar("foo").IsFloat());
Assert.AreEqual(-100.0f, scope.GetVar("foo").AsFloat());
}

[Test]
public void SetStringTest()
{
var scope = new Scope();

scope.SetVar("foo", "bar");
Assert.IsTrue(scope.GetVar("foo").IsString());
Assert.AreEqual("bar", scope.GetVar("foo").AsString());
}

[Test]
public void SetObjectTest()
{
var scope = new Scope();

var obj = new object();
scope.SetVar("foo", obj);
Assert.IsTrue(scope.GetVar("foo").IsObject());
Assert.AreEqual(obj, scope.GetVar("foo").AsObject());

scope.SetVar("foo", (object)true);
Assert.IsTrue(scope.GetVar("foo").IsBool());
Assert.IsTrue(scope.GetVar("foo").AsBool());

scope.SetVar("foo", (object)1);
Assert.IsTrue(scope.GetVar("foo").IsInt());
Assert.AreEqual(1, scope.GetVar("foo").AsInt());

scope.SetVar("foo", (object)1.0f);
Assert.IsTrue(scope.GetVar("foo").IsFloat());
Assert.AreEqual(1.0f, scope.GetVar("foo").AsFloat());

scope.SetVar("foo", (object)"bar");
Assert.IsTrue(scope.GetVar("foo").IsString());
Assert.AreEqual("bar", scope.GetVar("foo").AsString());
}

#endregion
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 445a11e

Please sign in to comment.