public void TestWeCanResetAdaptorCacheInvalidatedUponAdaptorReset() { string templates = "foo(x) ::= \"<x.id>: <x.name>\"\n"; writeFile(tmpdir, "foo.stg", templates); TemplateGroup group = new TemplateGroupFile(tmpdir + "/foo.stg"); group.RegisterModelAdaptor(typeof(User), new UserAdaptor()); group.GetModelAdaptor(typeof(User)); // get User, SuperUser into cache group.GetModelAdaptor(typeof(SuperUser)); group.RegisterModelAdaptor(typeof(User), new UserAdaptorConst()); // cache should be reset so we see new adaptor Template st = group.GetInstanceOf("foo"); st.Add("x", new User(100, "parrt")); string expecting = "const id value: const name value"; // sees UserAdaptorConst string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestSeesMostSpecificAdaptor() { string templates = "foo(x) ::= \"<x.id>: <x.name>\"\n"; writeFile(tmpdir, "foo.stg", templates); TemplateGroup group = new TemplateGroupFile(tmpdir + "/foo.stg"); group.RegisterModelAdaptor(typeof(User), new UserAdaptor()); group.RegisterModelAdaptor(typeof(SuperUser), new UserAdaptorConst()); // most specific Template st = group.GetInstanceOf("foo"); st.Add("x", new User(100, "parrt")); string expecting = "100: parrt"; string result = st.Render(); Assert.AreEqual(expecting, result); st.Remove("x"); st.Add("x", new SuperUser(100, "parrt")); expecting = "const id value: const name value"; // sees UserAdaptorConst result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestSimpleAdaptor() { string templates = "foo(x) ::= \"<x.id>: <x.name>\"\n"; writeFile(tmpdir, "foo.stg", templates); TemplateGroup group = new TemplateGroupFile(tmpdir + "/foo.stg"); group.RegisterModelAdaptor(typeof(User), new UserAdaptor()); Template st = group.GetInstanceOf("foo"); st.Add("x", new User(100, "parrt")); string expecting = "100: parrt"; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestAdaptorCoversSubclass() { string templates = "foo(x) ::= \"<x.id>: <x.name>\"\n"; writeFile(tmpdir, "foo.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "foo.stg")); group.RegisterModelAdaptor(typeof(User), new UserAdaptor()); Template st = group.GetInstanceOf("foo"); st.Add("x", new SuperUser(100, "parrt")); // create subclass of User string expecting = "100: super parrt"; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestAdaptorAndBadProp() { ErrorBufferAllErrors errors = new ErrorBufferAllErrors(); string templates = "foo(x) ::= \"<x.qqq>\"\n"; writeFile(tmpdir, "foo.stg", templates); TemplateGroup group = new TemplateGroupFile(tmpdir + "/foo.stg"); group.Listener = errors; group.RegisterModelAdaptor(typeof(User), new UserAdaptor()); Template st = group.GetInstanceOf("foo"); st.Add("x", new User(100, "parrt")); string expecting = ""; string result = st.Render(); Assert.AreEqual(expecting, result); TemplateRuntimeMessage msg = (TemplateRuntimeMessage)errors.Errors[0]; TemplateNoSuchPropertyException e = (TemplateNoSuchPropertyException)msg.Cause; Assert.AreEqual("User.qqq", e.PropertyName); }