summaryrefslogtreecommitdiff
path: root/Source/UnitTests/CoreTests/AbsyMetadata.cs
blob: 156f38b57a80a65ea2a5aa6c7df4a5c6b886fd2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Microsoft.Boogie;
using NUnit.Framework;
using System;

namespace CoreTests
{
  [TestFixture ()]
  public class AbsyMetadata {
    private Absy GetAbsy() {
      // The choice of Absy type here is arbitary
      return new AssertCmd(Token.NoToken, Expr.True);
    }

    [Test(), ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void NoMetadata() {
      var absy = GetAbsy();

      Assert.AreEqual(0, absy.NumberedMetaDataCount);

      string willFail = absy.GetMetadata<string>(0);
      // use willFail in someway so the result is live
      Assert.IsNotNullOrEmpty(willFail);
    }

    [Test(), ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void InvalidIndex() {
      var absy = GetAbsy();

      Assert.AreEqual(0, absy.NumberedMetaDataCount);
      absy.SetMetadata(0, "hello");

      string willFail = absy.GetMetadata<string>(1);
      // use willFail in someway so the result is live
      Assert.IsNotNullOrEmpty(willFail);
    }

    [Test()]
    public void SimpleMetadata() {
      var absy = GetAbsy();
      Assert.AreEqual(0, absy.NumberedMetaDataCount);

      var string1 = "hello";
      var string2 = "hello2";
      absy.SetMetadata(0, string1);
      Assert.AreEqual(1, absy.NumberedMetaDataCount);
      absy.SetMetadata(1, string2);
      Assert.AreEqual(2, absy.NumberedMetaDataCount);

      Assert.AreSame(string1, absy.GetMetadata<string>(0));
      Assert.AreSame(string2, absy.GetMetadata<string>(1));

      // Now try iterating over the metadata
      int count = 0;
      foreach (var o in absy.NumberedMetadata) {
          Assert.AreEqual(count, o.Key);
          Assert.IsNotNull(o.Value);
          ++count;
      }
      Assert.AreEqual(2, count);
    }

    [Test(), ExpectedException(typeof(InvalidCastException))]
    public void IncorrectType() {
      var absy = GetAbsy();
      Assert.AreEqual(0, absy.NumberedMetaDataCount);

      var string0 = "hello world";
      absy.SetMetadata(0, string0);

      // Now try retrive wrong type
      int wrongType = absy.GetMetadata<int>(0);
      // use "wrongType" is someway so the variable is live
      Assert.AreEqual(0, wrongType);
    }

    [Test()]
    public void NullPadding() {
      var absy = GetAbsy();
      Assert.AreEqual(0, absy.NumberedMetaDataCount);

      string foo = "foo";
      absy.SetMetadata<string>(10, foo);
      Assert.AreEqual(11, absy.NumberedMetaDataCount);

      // Should nulls be made accessible? Right now its impossible to access
      // them as we always throw an exception if the stored item is null
      for (int i = 0; i <= 9 ; ++i) {
        try {
          absy.GetMetadata<String>(i);
          Assert.Fail("Accesing unset metadata should of failed");
        }
        catch (InvalidCastException) {
        }
      }


      Assert.AreSame(foo, absy.GetMetadata<String>(10));
    }
  }
}