public void TestMaxAxlLoadParsing() { Kilogram result; Assert.AreEqual(true, TagExtensions.TryGetMaxAxleLoad(new TagsCollection(Tag.Create("maxaxleload", "30")), out result)); Assert.AreEqual(30 * 1000, result.Value); Assert.AreEqual(true, TagExtensions.TryGetMaxAxleLoad(new TagsCollection(Tag.Create("maxaxleload", "30.8")), out result)); Assert.AreEqual(30.8 * 1000, result.Value); Assert.AreEqual(true, TagExtensions.TryGetMaxAxleLoad(new TagsCollection(Tag.Create("maxaxleload", "30 t")), out result)); Assert.AreEqual(30 * 1000, result.Value); Assert.AreEqual(true, TagExtensions.TryGetMaxAxleLoad(new TagsCollection(Tag.Create("maxaxleload", "30.8 t")), out result)); Assert.AreEqual(30.8 * 1000, result.Value); Assert.AreEqual(false, TagExtensions.TryGetMaxAxleLoad(new TagsCollection(Tag.Create("max", "30.8 t")), out result)); }
/// <summary> /// Returns true if the given key value pair is relevant. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool IsRelevant(string key, string value) { KilometerPerHour speed; if (this.IsRelevant(key)) { // check the value. switch (key) { case "oneway": return(value == "yes" || value == "reverse" || value == "-1"); case "maxspeed": return(TagExtensions.TryParseSpeed(value, out speed)); } return(true); } return(false); }
/// <summary> /// Extends evey object with Should methods that is a basic, /// validation method. /// /// It uses special set of lambda metatags like: /// @be: /// It's used to indicate that a type or return type should be equal to something. /// /// @have: /// It's used on booleans and object. /// When it's called on objects properties it does null checks on those properties. /// When called on booleans it check if some return type or value is true. /// /// @not_be: /// negation of @be tag. /// /// @contain: /// It's used on collections and arrays to check for a specified item. /// /// @rcontain: /// It's used on collections and arrays to check for a specified item but using reflection matching /// instead of reference. /// /// @raise: [Class must implement Interface] /// It's used on methods to instrut that a certain set of parameter data /// should throw a specified exception. /// /// @not_raise: [Class must implement Interface] /// It's used on methods to instrut that a certain set of parameter data /// should not throw a specified exception. /// /// To enable strong typing use Tags namespace for strong typed methods. /// </summary> /// <example> /// /// // Object Extension example /// /// obj.Sum(1,1).Should(@be => 2); /// </example> /// <example> /// /// // Interface Extension example /// // Note: obj should implement some interface /// // in order for this extension to work /// /// obj.Sum(1,1).Should(@raise => typeof(MyException)); /// </example> /// <typeparam name="T"></typeparam> /// <typeparam name="N"></typeparam> /// <param name="obj"></param> /// <param name="operation"></param> /// <returns></returns> public static T Should <T, N>(this T obj, Expression <Func <T, N> > operation) { //parse the parameter names and make operations. switch (operation.Parameters[0].Name) { case "have": return(TagExtensions.Have(obj, operation.Compile()(obj))); case "be": return(TagExtensions.Be(obj, operation.Compile()(obj))); case "not_be": return(TagExtensions.NotBe(obj, operation.Compile()(obj))); case "contain": return(TagExtensions.Contain(obj, operation.Compile()(obj))); case "rcontain": return(TagExtensions.ReflectionContain(obj, operation.Compile()(obj))); case "raise": return(TagExtensions.Raise(obj, operation.Compile()(obj))); case "not_raise": return(TagExtensions.NotRaise(obj, operation.Compile()(obj))); } throw new CSpecException("Not a keyword"); }
public void TestSpeedParsing() { KilometerPerHour result; // Official Valid Values Assert.AreEqual(true, TagExtensions.TryParseSpeed("30", out result)); Assert.AreEqual(30, result.Value); Assert.AreEqual(true, TagExtensions.TryParseSpeed("30 mph", out result)); Assert.AreEqual(30, ((MilesPerHour)result).Value); Assert.AreEqual(true, TagExtensions.TryParseSpeed("30.8", out result)); Assert.AreEqual(30.8, result.Value); Assert.AreEqual(true, TagExtensions.TryParseSpeed("30.8 mph", out result)); Assert.AreEqual(30.8, ((MilesPerHour)result).Value); // Invalid Values Assert.AreEqual(false, TagExtensions.TryParseSpeed("2.3; 7'9\"", out result)); Assert.AreEqual(false, TagExtensions.TryParseSpeed("2,3", out result)); Assert.AreEqual(false, TagExtensions.TryParseSpeed("6'", out result)); Assert.AreEqual(false, TagExtensions.TryParseSpeed("6 ft", out result)); }
public void TestMaxSpeedParsing() { KilometerPerHour result; Assert.AreEqual(true, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("maxspeed", "30")), out result)); Assert.AreEqual(30, result.Value); Assert.AreEqual(true, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("maxspeed", "30.8")), out result)); Assert.AreEqual(30.8, result.Value); Assert.AreEqual(true, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("maxspeed", "30 mph")), out result)); Assert.AreEqual(30, ((MilesPerHour)result).Value); Assert.AreEqual(true, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("maxspeed", "30.8 mph")), out result)); Assert.AreEqual(30.8, ((MilesPerHour)result).Value); Assert.AreEqual(true, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("maxspeed", "30 knots")), out result)); Assert.AreEqual(30, ((Knots)result).Value); Assert.AreEqual(true, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("maxspeed", "30.8 knots")), out result)); Assert.AreEqual(30.8, ((Knots)result).Value); Assert.AreEqual(false, TagExtensions.TryGetMaxSpeed(new TagsCollection(Tag.Create("max", "30.8 knots")), out result)); }