PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/IronRuby.Tests/Parser/AssertTokenizer.cs

http://github.com/IronLanguages/main
C# | 237 lines | 188 code | 34 blank | 15 comment | 38 complexity | 0d89dbf4935d4fe735ac84c865a91f6e MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Diagnostics;
  17. using System.Dynamic;
  18. using Microsoft.Scripting;
  19. using IronRuby.Builtins;
  20. using IronRuby.Compiler;
  21. using IronRuby.Runtime;
  22. using System.IO;
  23. using System.Text;
  24. using System.Collections.Generic;
  25. namespace IronRuby.Tests {
  26. internal class AssertTokenizer {
  27. private readonly RubyContext/*!*/ _context;
  28. private readonly Tests/*!*/ _tests;
  29. private Tokenizer _tokenizer;
  30. private Tokens _actualToken;
  31. private TokenValue _actualValue;
  32. private SourceSpan _actualSpan;
  33. private LoggingErrorSink _log;
  34. private List<Tokens>/*!*/ _allTokens;
  35. private List<object>/*!*/ _allValues;
  36. public RubyCompatibility Compatibility { get; set; }
  37. public bool Verbatim { get; set; }
  38. public AssertTokenizer(Tests/*!*/ tests) {
  39. _log = new LoggingErrorSink();
  40. _tests = tests;
  41. _context = tests.Context;
  42. DefaultEncoding = RubyEncoding.UTF8;
  43. Compatibility = tests.Context.RubyOptions.Compatibility;
  44. }
  45. public Tokenizer Tokenizer {
  46. get { return _tokenizer; }
  47. }
  48. public List<Tokens>/*!*/ AllTokens {
  49. get { return _allTokens; }
  50. }
  51. public List<object>/*!*/ AllValues {
  52. get { return _allValues; }
  53. }
  54. public AssertTokenizer/*!*/ B {
  55. get { if (Debugger.IsAttached) Debugger.Break(); return this; }
  56. }
  57. public RubyEncoding/*!*/ DefaultEncoding { get; set; }
  58. public void EOF() {
  59. Read(Tokens.EndOfFile);
  60. Expect();
  61. }
  62. public AssertTokenizer/*!*/ Load(object/*!*/ source, Action<Tokenizer>/*!*/ tokenizerInit) { // source: byte[] or string
  63. var result = Load(source);
  64. tokenizerInit(result.Tokenizer);
  65. return result;
  66. }
  67. public AssertTokenizer/*!*/ Load(object/*!*/ source) { // source: byte[] or string
  68. _tests.Assert(_log.Errors.Count == 0, "Previous test case reported unexpected error/warning(s)");
  69. SourceUnit sourceUnit;
  70. RubyEncoding encoding;
  71. byte[] binarySource = source as byte[];
  72. if (binarySource != null) {
  73. encoding = RubyEncoding.Binary;
  74. sourceUnit = _context.CreateSourceUnit(new BinaryContentProvider(binarySource), null, encoding.Encoding, SourceCodeKind.File);
  75. } else {
  76. encoding = DefaultEncoding;
  77. sourceUnit = _context.CreateSnippet((string)source, SourceCodeKind.File);
  78. }
  79. _tokenizer = new Tokenizer(false, DummyVariableResolver.AllMethodNames) {
  80. ErrorSink = _log,
  81. Compatibility = Compatibility,
  82. Encoding = encoding,
  83. Verbatim = Verbatim,
  84. };
  85. _tokenizer.Initialize(sourceUnit);
  86. _allTokens = new List<Tokens>();
  87. _allValues = new List<object>();
  88. return this;
  89. }
  90. public AssertTokenizer/*!*/ Skip(int count) {
  91. while (count-- > 0) {
  92. Next();
  93. }
  94. return this;
  95. }
  96. public AssertTokenizer/*!*/ Next() {
  97. _actualToken = _tokenizer.GetNextToken();
  98. _actualValue = _tokenizer.TokenValue;
  99. _actualSpan = _tokenizer.TokenSpan;
  100. _allTokens.Add(_actualToken);
  101. _allValues.Add(_actualValue);
  102. return this;
  103. }
  104. public AssertTokenizer/*!*/ Read(Tokens token) {
  105. Next();
  106. _tests.Assert(_actualToken == token);
  107. return this;
  108. }
  109. public AssertTokenizer/*!*/ Read(int expected) {
  110. Next();
  111. _tests.Assert(_actualToken == Tokens.Integer);
  112. _tests.Assert(expected == _actualValue.Integer1);
  113. return this;
  114. }
  115. public AssertTokenizer/*!*/ Read(string/*!*/ expected) {
  116. Next();
  117. _tests.Assert(_actualToken == Tokens.StringContent);
  118. _tests.Assert(_actualValue.StringContent is string);
  119. _tests.Assert(expected == (string)_actualValue.StringContent);
  120. return this;
  121. }
  122. public AssertTokenizer/*!*/ Read(byte[]/*!*/ expected) {
  123. Next();
  124. _tests.Assert(_actualToken == Tokens.StringContent);
  125. _tests.Assert(_actualValue.StringContent is byte[]);
  126. _tests.Assert(expected.ValueCompareTo(expected.Length, (byte[])_actualValue.StringContent) == 0);
  127. return this;
  128. }
  129. public AssertTokenizer/*!*/ ReadSymbol(Tokens token, string expected) {
  130. Next();
  131. _tests.Assert(_actualToken == token);
  132. _tests.Assert(expected == _actualValue.String);
  133. return this;
  134. }
  135. public AssertTokenizer/*!*/ Read(RubyRegexOptions expected) {
  136. Next();
  137. _tests.Assert(_actualToken == Tokens.RegexpEnd);
  138. _tests.Assert(expected == _actualValue.RegExOptions);
  139. return this;
  140. }
  141. public AssertTokenizer/*!*/ ReadBigInteger(string/*!*/ expected, int @base) {
  142. Next();
  143. _tests.Assert(_actualToken == Tokens.BigInteger);
  144. _tests.Assert(StringComparer.OrdinalIgnoreCase.Compare(_actualValue.BigInteger.ToString(@base), expected) == 0);
  145. return this;
  146. }
  147. public AssertTokenizer/*!*/ Read(double expected) {
  148. Next();
  149. _tests.Assert(_actualToken == Tokens.Float);
  150. if (Double.IsNaN(expected)) {
  151. _tests.Assert(Double.IsNaN(_actualValue.Double));
  152. } else if (Double.IsNegativeInfinity(expected)) {
  153. _tests.Assert(Double.IsNegativeInfinity(_actualValue.Double));
  154. } else if (Double.IsPositiveInfinity(expected)) {
  155. _tests.Assert(Double.IsPositiveInfinity(_actualValue.Double));
  156. } else {
  157. // TODO: is this correct?
  158. _tests.Assert(System.Math.Abs(_actualValue.Double - expected) < Double.Epsilon);
  159. }
  160. return this;
  161. }
  162. public AssertTokenizer/*!*/ Expect(params ErrorInfo[] errors) {
  163. if (errors == null || errors.Length == 0) {
  164. _tests.Assert(_log.Errors.Count == 0, "Unexpected error/warning(s)");
  165. } else {
  166. _tests.Assert(_log.Errors.Count == errors.Length, String.Format("Expected {0} error/warning(s)", errors.Length));
  167. for (int i = 0; i < errors.Length; i++) {
  168. _tests.Assert(_log.Errors[i].Code == errors[i].Code);
  169. }
  170. }
  171. _log.Errors.Clear();
  172. return this;
  173. }
  174. public AssertTokenizer/*!*/ State(LexicalState expected) {
  175. _tests.Assert(Tokenizer.LexicalState == expected);
  176. return this;
  177. }
  178. public AssertTokenizer/*!*/ this[Tokens expected] {
  179. get { return Read(expected); }
  180. }
  181. public AssertTokenizer/*!*/ this[string/*!*/ expected] {
  182. get { return Read(expected); }
  183. }
  184. public AssertTokenizer/*!*/ this[string/*!*/ expected, Encoding/*!*/ encoding] {
  185. get { return Read(encoding.GetBytes(expected)); }
  186. }
  187. public AssertTokenizer/*!*/ this[byte[]/*!*/ expected] {
  188. get { return Read(expected); }
  189. }
  190. public AssertTokenizer/*!*/ this[int expected] {
  191. get { return Read(expected); }
  192. }
  193. public AssertTokenizer/*!*/ this[Tokens token, string/*!*/ expected] {
  194. get {
  195. Next();
  196. _tests.Assert(_actualToken == token);
  197. _tests.Assert(_actualValue.StringContent is string);
  198. _tests.Assert(expected == (string)_actualValue.StringContent);
  199. return this;
  200. }
  201. }
  202. }
  203. }