123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package neighbors
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func TestNewState(t *testing.T) {
- s := NewState()
- assert.Equal(t, &state{present: false, was: false, initial: true}, s)
- }
- func TestState_Present(t *testing.T) {
- cases := []struct {
- name string
- s State
- exp bool
- }{
- {
- name: "true",
- s: &state{present: true},
- exp: true,
- },
- {
- name: "false",
- s: &state{present: false},
- },
- }
- for _, tc := range cases {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
- assert.Equal(t, tc.exp, tc.s.Present())
- })
- }
- }
- func TestState_Changed(t *testing.T) {
- cases := []struct {
- name string
- s State
- exp bool
- }{
- {
- name: "true to true",
- s: &state{present: true, was: true},
- exp: false,
- },
- {
- name: "true to false",
- s: &state{present: false, was: true},
- exp: true,
- },
- {
- name: "false to true",
- s: &state{present: true, was: false},
- exp: true,
- },
- {
- name: "false to false",
- s: &state{present: false, was: false},
- exp: false,
- },
- }
- for _, tc := range cases {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
- assert.Equal(t, tc.exp, tc.s.Changed())
- })
- }
- }
- func TestState_Set(t *testing.T) {
- cases := []struct {
- name string
- s, exp State
- p bool
- }{
- {
- name: "initial to true",
- s: &state{initial: true},
- p: true,
- exp: &state{present: true, was: false, initial: false},
- },
- {
- name: "initial to false",
- s: &state{initial: true},
- p: false,
- exp: &state{present: false, was: true, initial: false},
- },
- {
- name: "true to true",
- s: &state{present: true},
- p: true,
- exp: &state{present: true, was: true},
- },
- {
- name: "true to false",
- s: &state{present: true},
- p: false,
- exp: &state{present: false, was: true},
- },
- {
- name: "false to true",
- s: &state{present: false, was: true},
- p: true,
- exp: &state{present: true, was: false},
- },
- {
- name: "false to false",
- s: &state{present: false, was: true},
- p: false,
- exp: &state{present: false, was: false},
- },
- }
- for _, tc := range cases {
- tc := tc
- t.Run(tc.name, func(t *testing.T) {
- t.Parallel()
- tc.s.Set(tc.p)
- assert.Equal(t, tc.exp, tc.s)
- })
- }
- }
- func TestState_Reset(t *testing.T) {
- s := &state{initial: false}
- s.Reset()
- assert.Equal(t, &state{initial: true}, s)
- }
|