Module: Waxx::Test

Extended by:
Test
Included in:
Test
Defined in:
waxx/test.rb

Overview

The test framework for waxx

## Usage:

“` module Waxx

def test_waxx_app
  # Setup x vars for different situations
  Waxx::App.init
  x_guest = Waxx::Test.x_nonuser
  x_admin = Waxx::Test.x_user
  x_admin.usr['grp'] = ["admin"]
  handler = {
    test: {
      desc: "A test handler",
      get: -> (x, n){
        "get #{n}"
      }
    }
  }

  Waxx::Test.test(Waxx::App, # Module to test

    "access?" => {  # Method to test. 
      # Each item below is a "test-name": [result value, 
      #   expect: extepected-value, 
      #   run: Proc to run that returns true on success, 
      #   args: args to pass to the module method
      # ]
      "nil" => [Waxx::App.access?(x_guest), expect: true],
      "asterisk" => [Waxx::App.access?(x_guest, acl:"*"), expect: true],
      "all" => [Waxx::App.access?(x_guest, acl:"all"), expect: true],
      "any" => [Waxx::App.access?(x_guest, acl:"any"), expect: true],
      "user" => [Waxx::App.access?(x_guest, acl:"user"), expect: false],
      "non-user-admin" => [Waxx::App.access?(x_guest, acl:"admin"), expect: false],
      "non-user-array-of-admin-other" => [Waxx::App.access?(x_guest, acl:%w(admin other)), expect: false],
      "proc-true" => [Waxx::App.access?(x_guest, acl: ->(x){true}), expect: true],
      "proc-false" => [Waxx::App.access?(x_guest, acl: ->(x){false}), expect: false],
      "admin-admin" => [Waxx::App.access?(x_admin, acl:"admin"), expect: true],
      "admin-array-of-admin-other" => [Waxx::App.access?(x_admin, acl:%w(admin other)), expect: true],
    },

    "not_found" => {
      "message" => [Waxx::App.not_found(x_guest, message:"not found"), run: -> (x) {x.res == "not found"}, args: [x_guest]]
    },

    "runs" => {
      "with-opts" => [(Waxx::App.init; Waxx::App[:test_app_handler] = handler), expect: handler],
      "with-name" => [Waxx::App[:test_app_handler], expect: handler],
      "run" => [Waxx::App.run(x_guest, :test_app_handler, :test, :get, [1]), expect: "get 1"],
    }

  )

end “`

Instance Method Summary collapse

Instance Method Details

#mock_reqObject

A mock request skeleton. Create and then edit with test-specific attrs



114
115
116
# File 'waxx/test.rb', line 114

def mock_req
  Waxx::Req.new(ENV, {}, 'GET', "/test/test/1", {}, {}, {}, Time.new).freeze
end

#mock_res(req) ⇒ Object

A mock request with default values



119
120
121
# File 'waxx/test.rb', line 119

def mock_res(req)
  Waxx::Res.new("", 200, Waxx::Server.default_response_headers(req, "txt"), [], [], [])
end

#run_test(got, expect: nil, run: nil, args: []) ⇒ Object

Run the test



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'waxx/test.rb', line 96

def run_test(got, expect:nil, run: nil, args:[])
  if run
    expect = true
    got = run.call(*args)
  end
  if got == expect
    {"status" => "pass"}
  else
    {"status" => "fail",
      "error" => {
        "got" => got,
        "expect" => expect
      }
    }
  end
end

#test(module_name, methods = {}) ⇒ Object

Setup and process a test on a module method



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
# File 'waxx/test.rb', line 66

def test(module_name, methods={})
  mod = module_name.to_s
  re = {mod => {}}
  methods.each{|meth, tests|
    re[mod][meth] = {}
    passed = 0
    tests.each{|test_name, args|
      begin
        re[mod][meth][test_name] = run_test(*args)
        passed += 1 if re[mod][meth][test_name]['status'] == 'pass'
      rescue => test_error
        re[mod][meth][test_name] = {
          "status" => "fail",
          "error" => {
            "got" => "ERROR",
            "message" => test_error.to_s,
            "backtrace" => test_error.backtrace
          }
        }
      end
    }
    re[mod][meth]['tests'] = tests.size
    re[mod][meth]['tests_passed'] = passed
    re[mod][meth]['test_performance'] = "#{((passed.to_f / tests.size) * 100).to_i}%"
  }
  re
end

#x_nonuser(req = mock_req, res = nil, db = nil) ⇒ Object

A non-user/public/guest



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'waxx/test.rb', line 144

def x_nonuser(req=mock_req, res=nil, db=nil) 
   res ||= mock_res(req)
   Waxx::X.new(
		req,
		res,
		{'grp'=>[]},
		{},
		db,
		:get,
		:test,
		:test,
		1,
		[1],
		"json",
		[]
	)
end

#x_user(req = mock_req, res = nil, db = nil) ⇒ Object

A logged-in user



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'waxx/test.rb', line 124

def x_user(req=mock_req, res=nil, db=nil) 
   res ||= mock_res(req)
   #x = Waxx::X.new(req, res, usr, ua, db, meth.downcase.to_sym, app, act, oid, args, ext, jobs).freeze
   Waxx::X.new(
		req,
		res,
		{'id' => 1, 'grp'=>['user']},
		{'id' => 1},
		db,
		:get,
		:test,
		:test,
		1,
		[1],
		"json",
		[]
	)
end