...

Text file src/edge-infra.dev/third_party/lua/simple_test/test.lua

Documentation: edge-infra.dev/third_party/lua/simple_test

     1local test = require 'simple_test'
     2local utils = require 'utils'
     3
     4test('assert.equal', function(a)
     5  a.equal('a', 'a')
     6end)
     7
     8test('assert.not_equal', function(a)
     9  a.not_equal('a', 'b')
    10end)
    11
    12test('assert.ok', function(a)
    13  a.ok(true)
    14  a.ok('lua')
    15end)
    16
    17test('assert.not_ok', function(a)
    18  a.not_ok(false)
    19  a.not_ok(nil)
    20end)
    21
    22test('assert.throw (no pattern)', function(a)
    23  local method = function(first, last)
    24    assert(first == last, 'invalid!')
    25  end
    26
    27  a.throw(method, { 'a', 'b' })
    28end)
    29
    30test('assert.throw (with pattern)', function(a)
    31  local method = function(first, last)
    32    assert(first == last, 'invalid!')
    33  end
    34
    35  a.throw(method, { 'a', 'b' }, 'invalid!')
    36end)
    37
    38test('assert.throw (pattern not matched)', function(a)
    39  local method = function(first, last)
    40    assert(first == last, 'invalid!')
    41  end
    42
    43  a.throw(method, { 'a', 'b' }, 'foo')
    44end, true)
    45
    46test('assert.throw (does not throw)', function(a)
    47  local method = function(_, _) end
    48
    49  a.throw(method, { 'a', 'b' })
    50end, true)
    51
    52test('assert.throw (does not throw but still has pattern arg)', function(a)
    53  local method = function(_, _) end
    54
    55  a.throw(method, { 'a', 'b' }, "foo")
    56end, true)
    57
    58test('assert.delta', function(a)
    59  a.delta(0.3, 0.1 + 0.2)
    60end)
    61
    62test('assert.deep_equal', function(a)
    63  local obj1 = {
    64    favorites = {
    65      languages = {
    66        'lua',
    67        'javascript',
    68        'c'
    69      }
    70    }
    71  }
    72
    73  local obj2 = {
    74    favorites = {
    75      languages = {
    76        'lua',
    77        'javascript',
    78        'c'
    79      }
    80    }
    81  }
    82
    83  a.deep_equal(obj1, obj2)
    84end)
    85
    86test('utils.is_deep_equal', function(a)
    87  a.not_ok(is_deep_equal('a', 'b'))
    88  a.ok(is_deep_equal('a', 'a'))
    89  a.not_ok(is_deep_equal({ a = { 'b' } }, { a = { 'c' } }))
    90  a.ok(is_deep_equal({ a = { 'b' } }, { a = { 'b' } }))
    91  a.ok(is_deep_equal({ 'a', 'b', 'c', 'd' }, { 'a', 'b', 'c', 'd' }))
    92  a.not_ok(is_deep_equal({ 'a', 'b', 'c', 'd' }, { 'a', 'b', 'c', 'e' }))
    93end)
    94
    95test('utils.is_deep_equal (reference cycles, different graph shape)', function(a)
    96  -- structurally equivalent but nested table t is referenced twice in obj1,
    97  -- but copied in obj2
    98  local t = { d = "foo" }
    99
   100  local obj1 = {
   101    a = { c = t },
   102    b = t
   103  }
   104
   105  local obj2 = {
   106    a = { c = { d = "foo" } },
   107    b = { d = "foo" }
   108  }
   109
   110  a.ok(is_deep_equal(obj1, obj2))
   111end)
   112
   113test("utils.is_deep_equal (reference cycles, same graph shape)", function(a)
   114  -- tables both contain a self reference to the starting table. they are completely
   115  -- separated graphs
   116  local table_a1 = {}
   117  local table_b1 = {}
   118  table_a1.b = table_b1
   119  table_b1.a = table_a1
   120
   121  local table_a2 = {}
   122  local table_b2 = {}
   123  table_a2.b = table_b2
   124  table_b2.a = table_a2
   125
   126  a.ok(is_deep_equal(table_a1, table_a2))
   127end)
   128
   129test("utils.is_deep_equal (reference cycles, connected graphs)", function(a)
   130  -- tables form a simple reference cycle and are part of the same connected
   131  -- graph. because it is a symmetrical graph, they are still structurally
   132  -- equivalent
   133  local table_a = {}
   134  local table_b = {}
   135  table_a.ref = table_b
   136  table_b.ref = table_a
   137
   138  a.ok(is_deep_equal(table_a, table_b))
   139end)

View as plain text