public static char ESCAPE_MARK = '\\'; //转义符号 public Element Read(ExpressionReader sr) { int index = sr.GetCurrentIndex(); var sb = new StringBuilder(); int b = sr.Read(); if (b == -1 || b != START_MARK) { throw new FormatException("不是有效的字符窜开始"); } while ((b = sr.Read()) != -1) { char c = (char)b; if (c == ESCAPE_MARK) {//遇到转义字符 c = GetEscapeValue((char)sr.Read()); } else if (c == END_MARK) {//遇到非转义的引号 return(new Element(sb.ToString(), index, ElementType.STRING)); } sb.Append(c); } throw new FormatException("不是有效的字符窜结束"); }
public Element Read(ExpressionReader sr) { int index = sr.GetCurrentIndex(); StringBuilder sb = new StringBuilder(); int b = sr.Read(); if (b == -1 || b != START_MARK) { throw new FormatException("不是有效的时间开始"); } while ((b = sr.Read()) != -1) { char c = (char)b; if (c == END_MARK) { return(new Element(FormatTime(sb.ToString()), index, ElementType.DATE)); } if (DATE_CHARS.IndexOf(c) == -1) { throw new FormatException("时间类型不能包函非法字符:" + c); } sb.Append(c); } throw new FormatException("不是有效的时间结束"); }
public Element Read(ExpressionReader sr) { int index = sr.GetCurrentIndex(); var sb = new StringBuilder(); int b = sr.Read(); if (b == -1) { throw new FormatException("表达式已结束"); } char c = (char)b; sb.Append(c); if (IsOperatorWord(sb.ToString())) { if (sb.Length == 1) {//两个符号的运算符优先,如<=,不应该认为是<运算符 sr.Mark(0); b = sr.Read(); if (b != -1) { if (IsOperatorWord(sb.ToString() + (char)b)) { return(new Element(sb.ToString() + (char)b, index, ElementType.OPERATOR)); } } sr.Reset(); } return(new Element(sb.ToString(), index, ElementType.OPERATOR)); } while ((b = sr.Read()) != -1) { c = (char)b; sb.Append(c); if (IsOperatorWord(sb.ToString())) { return(new Element(sb.ToString(), index, ElementType.OPERATOR)); } if (VariableTypeReader.STOP_CHAR.IndexOf(c) >= 0) {//单词停止符 throw new FormatException("不是有效的运算符:" + sb.ToString()); } } throw new FormatException("不是有效的运算符结束"); }
private void AssertItemsEqual(params string[] items) { foreach (var item in items) { Assert.Equal(item, reader.Read()); } }
public void ReadNullActionResultThrowsException() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = null; Assert.Throws<ArgumentNullException>(() => reader.Read(args)); }
private int GetChar() { int chr; if (_putbackChar != -1) { chr = _putbackChar; _putbackChar = -1; } else { chr = _reader.Read(); } return(chr); }
public void ReadNullObjectThrowsException() { var reader = new ExpressionReader(); Expression <Func <TestApiController, object> > args = null; Assert.Throws <ArgumentNullException>(() => reader.Read(args)); }
public static string DOUBLE_MARKS = "dD"; //double的结尾标志 public Element Read(ExpressionReader sr) { int index = sr.GetCurrentIndex(); string s = string.Empty; int b = -1; while ((b = sr.Read()) != -1) { char c = (char)b; if (NUMBER_CHARS.IndexOf(c) == -1) { if (LONG_MARKS.IndexOf(c) >= 0) { if (s.IndexOf(".") >= 0) {//有小数点 throw new FormatException("long类型不能有小数点"); } return(new Element(s.ToString(), index, ElementType.LONG)); } else if (FLOAT_MARKS.IndexOf(c) >= 0) { CheckDecimal(s); return(new Element(s.ToString(), index, ElementType.FLOAT)); } else if (DOUBLE_MARKS.IndexOf(c) >= 0) { CheckDecimal(s); return(new Element(s.ToString(), index, ElementType.DOUBLE)); } else { sr.Reset(); if (s.IndexOf(".") >= 0) {//没有结束标志,有小数点,为double CheckDecimal(s); return(new Element(s.ToString(), index, ElementType.DOUBLE)); } else {//没有结束标志,无小数点,为int return(new Element(s.ToString(), index, ElementType.INT)); } } } s += c; sr.Mark(0); } //读到结未 if (s.IndexOf(".") >= 0) {//没有结束标志,有小数点,为double CheckDecimal(s); return(new Element(s.ToString(), index, ElementType.DOUBLE)); } else {//没有结束标志,无小数点,为int return(new Element(s.ToString(), index, ElementType.INT)); } }
public void ReadDoesNotGetAreaName() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); Assert.That(result.ContainsKey("area"), Is.False); }
public void ReadDoesNotGetAreaNameWHenNotPresent() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); Assert.That(result.Area, Is.Empty); }
public void ReadDoesNotGetAreaNameWHenNotPresent() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.Index(); var result = reader.Read(args); Assert.That(result.Area, Is.Empty); }
public void ReadReturnsDictionary() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.Index(); var result = reader.Read(args); Assert.That(result, Is.Not.Null); Assert.That(result.Count, Is.GreaterThan(0)); }
public void ReadReturnsValidRouteValues() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.Index(); var result = reader.Read(args); result.CheckDataOk(); Assert.That(result.DataOk, Is.True); }
public void ReadGetsApiControllerAndAction() { var reader = new ExpressionReader(); Expression <Func <TestApiController, object> > args = c => c.Get(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("TestApi")); Assert.That(result.Action, Is.EqualTo("Get")); }
public void ReadGetsActionFromAttributeWhenPresent() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.MethodName(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Test")); Assert.That(result.Action, Is.EqualTo("ActionNameAttribute")); }
public void ReadGetsControllerAndAction() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.Index(); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("Test")); Assert.That(result["action"], Is.EqualTo("Index")); }
public void ReadGetsApiControllerAndAction() { var reader = new ExpressionReader(); Expression<Func<TestApiController, object>> args = c => c.Get(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("TestApi")); Assert.That(result.Action, Is.EqualTo("Get")); }
public void CanReadVoidMethod() { var reader = new ExpressionReader(); Expression <Action <MemberController> > args = c => c.DoNothing(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Member")); Assert.That(result.Action, Is.EqualTo("DoNothing")); }
public void ReadGetsControllerAndAction() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("Test")); Assert.That(result["action"], Is.EqualTo("Index")); }
public void CanReadVoidMethod() { var reader = new ExpressionReader(); Expression<Action<MemberController>> args = c => c.DoNothing(); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("Member")); Assert.That(result["action"], Is.EqualTo("DoNothing")); }
public void ReadReturnsDictionary() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); Assert.That(result, Is.Not.Null); Assert.That(result.Count, Is.GreaterThan(0)); }
public void ReadGetsMethodParams() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.GetItem(42); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Test")); Assert.That(result.Action, Is.EqualTo("GetItem")); Assert.That(result.Values.ValueByName("id"), Is.EqualTo(42)); }
public void ReadGetsAreaNameWhenPresent() { var reader = new ExpressionReader(); Expression<Func<Areas.SomeArea.Controllers.TestController, ActionResult>> args = c => c.Index(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Test")); Assert.That(result.Action, Is.EqualTo("Index")); Assert.That(result.Area, Is.EqualTo("SomeArea")); }
public void ReadGetsAreaNameWhenPresent() { var reader = new ExpressionReader(); Expression <Func <Areas.SomeArea.Controllers.TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Test")); Assert.That(result.Action, Is.EqualTo("Index")); Assert.That(result.Area, Is.EqualTo("SomeArea")); }
public void ReadGetsMethodParam() { var reader = new ExpressionReader(); Expression<Func<TestApiController, object>> args = c => c.Post(42); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("TestApi")); Assert.That(result["action"], Is.EqualTo("Post")); Assert.That(result["id"], Is.EqualTo("42")); }
public void ReadReturnsValidRouteValues() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); result.CheckDataOk(); Assert.That(result.DataOk, Is.True); }
public void ReadGetsMethodParams() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.GetItem(42); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Test")); Assert.That(result.Action, Is.EqualTo("GetItem")); Assert.That(result.Values.ValueByName("id"), Is.EqualTo(42)); }
public void ReadGetsMethodParams() { var reader = new ExpressionReader(); Expression <Func <TestController, ActionResult> > args = c => c.GetItem(42); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("Test")); Assert.That(result["action"], Is.EqualTo("GetItem")); Assert.That(result["id"], Is.EqualTo("42")); }
public void ReadGetsMethodParam() { var reader = new ExpressionReader(); Expression <Func <TestApiController, object> > args = c => c.Post(42); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("TestApi")); Assert.That(result["action"], Is.EqualTo("Post")); Assert.That(result["id"], Is.EqualTo("42")); }
public void ReadGetsAreaName() { var reader = new ExpressionReader(); Expression <Func <Areas.SomeArea.TestController, ActionResult> > args = c => c.Index(); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("Test")); Assert.That(result["action"], Is.EqualTo("Index")); Assert.That(result["area"], Is.EqualTo("SomeArea")); }
public void ReadGetsMethodParam() { var reader = new ExpressionReader(); Expression <Func <TestApiController, object> > args = c => c.Post(42); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("TestApi")); Assert.That(result.Action, Is.EqualTo("Post")); Assert.That(result.Values.FindByName("id").Value, Is.EqualTo(42)); }
public void ReadGetsMethodParams() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.GetItem(42); var result = reader.Read(args); Assert.That(result["controller"], Is.EqualTo("Test")); Assert.That(result["action"], Is.EqualTo("GetItem")); Assert.That(result["id"], Is.EqualTo("42")); }
public void ReadGetsMethodParam() { var reader = new ExpressionReader(); Expression<Func<TestApiController, object>> args = c => c.Post(42); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("TestApi")); Assert.That(result.Action, Is.EqualTo("Post")); Assert.That(result.Values.FindByName("id").Value, Is.EqualTo(42)); }
public static string SPLITOR_CHAR = "(),";//所有分割符 public Element Read(ExpressionReader sr) { int index = sr.GetCurrentIndex(); int b = sr.Read(); char c = (char)b; if (b == -1 || SPLITOR_CHAR.IndexOf(c) == -1) { throw new FormatException("不是有效的分割字符"); } return(new Element(c.ToString(), index, ElementType.SPLITOR)); }
public static char END_MARK = '('; //函数结束 /** * 从流中读取函数类型的ExpressionToken * @param sr * @return * @throws FormatException * @throws IOException */ public Element Read(ExpressionReader sr) { int index = sr.GetCurrentIndex(); var sb = new StringBuilder(); int b = sr.Read(); if (b == -1 || b != START_MARK) { throw new FormatException("不是有效的函数开始"); } bool readStart = true; while ((b = sr.Read()) != -1) { char c = (char)b; if (c == END_MARK) { if (sb.Length == 0) { throw new FormatException("函数名称不能为空"); } sr.Reset(); return(new Element(sb.ToString(), index, ElementType.FUNCTION)); } //if (!Character.isJavaIdentifierPart(c)) { // throw new FormatException("名称不能为非法字符:" + c); //} if (readStart) { //if (!Character.isJavaIdentifierStart(c)) { // throw new FormatException("名称开头不能为字符:" + c); //} readStart = false; } sb.Append(c); sr.Mark(0); } throw new FormatException("不是有效的函数结束"); }
/// <summary> /// 根据流开头构造不同的词元读取 流应该非空格开头 /// </summary> /// <param name="reader"></param> /// <returns></returns> public static IElementReader CreateElementReader(ExpressionReader reader) { //读一个char reader.Mark(0); int b = reader.Read(); reader.Reset(); if (b != -1) { char c = (char)b; try { if (c == StringTypeReader.START_MARK) {//"开头,构造字符串读取器 return(new StringTypeReader()); } else if (c == DateTypeReader.START_MARK) {//[开头,构造日期读取器 return(new DateTypeReader()); } else if (c == FunctionTypeReader.START_MARK) {//$开头,构造函数读取器 return(new FunctionTypeReader()); } else if (SplitorTypeReader.SPLITOR_CHAR.IndexOf(c) >= 0) {//如果是分隔符,构造分隔符读取器 return(new SplitorTypeReader()); } else if (NumberTypeReader.NUMBER_CHARS.IndexOf(c) >= 0) {//以数字开头,构造数字类型读取器 return(new NumberTypeReader()); } else if (OperatorTypeReader.IsOperatorStart(reader)) {//如果前缀是运算符,构造运算符读取器 return(new OperatorTypeReader()); } else { return(new VariableTypeReader());//否则构造一个变量读取器 } } catch (Exception e) { throw new FormatException("", e); } } else { throw new FormatException("流已结束"); } }
/// <summary> /// 测试是否为运算符 /// </summary> /// <param name="sr"></param> /// <returns></returns> public static bool IsOperatorStart(ExpressionReader sr) { sr.Mark(0); try { var sb = new StringBuilder(); int b = sr.Read(); if (b == -1) { return(false); } char c = (char)b; sb.Append(c); if (IsOperatorWord(sb.ToString())) { return(true); } while ((b = sr.Read()) != -1) { c = (char)b; sb.Append(c); if (IsOperatorWord(sb.ToString())) { return(true); } if (VariableTypeReader.STOP_CHAR.IndexOf(c) >= 0) {//单词停止符 return(false); } } return(false); } finally { sr.Reset(); } }
public void ReadApiReturnsRouteValues() { var reader = new ExpressionReader(); Expression <Func <TestApiController, object> > args = c => c.Get(); var result = reader.Read(args); Assert.That(result, Is.Not.Null); Assert.That(result.Controller, Is.EqualTo("TestApi")); Assert.That(result.Action, Is.EqualTo("Get")); Assert.That(result.Area, Is.Empty); Assert.That(result.Values, Is.Not.Null); Assert.That(result.Values.Count, Is.EqualTo(0)); }
public void ReadApiReturnsRouteValues() { var reader = new ExpressionReader(); Expression<Func<TestApiController, object>> args = c => c.Get(); var result = reader.Read(args); Assert.That(result, Is.Not.Null); Assert.That(result.Controller, Is.EqualTo("TestApi")); Assert.That(result.Action, Is.EqualTo("Get")); Assert.That(result.Area, Is.Empty); Assert.That(result.Values, Is.Not.Null); Assert.That(result.Values.Count, Is.EqualTo(0)); }
private string ReadWord(ExpressionReader sr) { var sb = new StringBuilder(); bool readStart = true; int b = -1; while ((b = sr.Read()) != -1) { char c = (char)b; if (STOP_CHAR.IndexOf(c) >= 0 && !readStart) {//单词停止符,并且忽略第一个字符 sr.Reset(); return(sb.ToString()); } if (readStart) { readStart = false; } sb.Append(c); sr.Mark(0); } return(sb.ToString()); }
public void ReadGetsActionFromAttributeWhenPresent() { var reader = new ExpressionReader(); Expression<Func<TestController, ActionResult>> args = c => c.MethodName(); var result = reader.Read(args); Assert.That(result.Controller, Is.EqualTo("Test")); Assert.That(result.Action, Is.EqualTo("ActionNameAttribute")); }
// 空测试 public void EmptyTest() { reader = new ExpressionReader(""); Assert.Equal("", reader.Read()); }
// 整数测试 public void NumberTest() { reader = new ExpressionReader("1"); Assert.Equal("1", reader.Read()); }