~alexreg/mathtexdotnet/0.1

« back to all changes in this revision

Viewing changes to TexDotNet.Tests/TestExamplesReader.cs

  • Committer: Alex Regueiro
  • Date: 2009-09-25 19:25:01 UTC
  • Revision ID: alexreg@gmail.com-20090925192501-lyu4daug4m0f6he7
Rewrote testing framework. Tests work on principle that if you parse an expression, write it back, and parse it again, the two expression trees should always be equivalent.
Fixed bug in TexComposer involving  brackets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using System;
2
 
using System.Collections.Generic;
3
 
using System.IO;
4
 
using System.Linq;
5
 
using System.Text;
6
 
 
7
 
namespace TexDotNet.Tests
8
 
{
9
 
    using TokenStream = IEnumerable<TexToken>;
10
 
 
11
 
    public class TestExamplesReader : IDisposable
12
 
    {
13
 
        private StreamReader streamReader;
14
 
 
15
 
        private bool disposed = false;
16
 
 
17
 
        public TestExamplesReader(string path)
18
 
            : this(new FileStream(path, FileMode.Open, FileAccess.Read))
19
 
        {
20
 
        }
21
 
 
22
 
        public TestExamplesReader(Stream stream)
23
 
        {
24
 
            this.BaseStream = stream;
25
 
            this.streamReader = new StreamReader(this.BaseStream);
26
 
        }
27
 
 
28
 
        ~TestExamplesReader()
29
 
        {
30
 
            GC.SuppressFinalize(true);
31
 
            Dispose(false);
32
 
        }
33
 
 
34
 
        public Stream BaseStream
35
 
        {
36
 
            get;
37
 
            private set;
38
 
        }
39
 
 
40
 
        public void Dispose()
41
 
        {
42
 
            Dispose(true);
43
 
        }
44
 
 
45
 
        protected void Dispose(bool disposing)
46
 
        {
47
 
            if (!disposed)
48
 
            {
49
 
                if (disposing)
50
 
                {
51
 
                    this.streamReader.Dispose();
52
 
                    this.BaseStream.Dispose();
53
 
                }
54
 
            }
55
 
 
56
 
            disposed = true;
57
 
        }
58
 
 
59
 
        public IEnumerable<TestExample> ReadAllExamples()
60
 
        {
61
 
            TestExample curItem;
62
 
            while ((curItem = ReadExample()) != null)
63
 
                yield return curItem;
64
 
        }
65
 
 
66
 
        public TestExample ReadExample()
67
 
        {
68
 
            var text = streamReader.ReadLine();
69
 
            if (text == null)
70
 
                return null;
71
 
 
72
 
            var expectedTokenString = streamReader.ReadLine();
73
 
            var expectedTokens = expectedTokenString.Split(' ').Select(tokenString =>
74
 
                {
75
 
                    return TexToken.FromSymbol(TexSymbolKind.Unknown, -1, null);
76
 
                }).ToArray();
77
 
 
78
 
            streamReader.ReadLine();
79
 
 
80
 
            return new TestExample(text, expectedTokens);
81
 
        }
82
 
    }
83
 
}