func Test_Run_creates_channel(t *testing.T) { var out chan Person junction.New(&out, nil) if out == nil { t.Error("Channel is nil") } }
func Test_update_via_member_func_yields_updated_value(t *testing.T) { s := make(chan string) var out chan Person junction.New(&out, []junction.Source{{ Input: s, Update: (*Person).SetName, Model: &Person{Name: "Jeff", Age: 56}, }}) s <- "Fishy Bob" person := <-out fb := Person{Age: 56, Name: "Fishy Bob"} if person != fb { t.Errorf("Where's Fishy Bob?") } }
func Test_update_via_non_member_func_yields_updated_value(t *testing.T) { i := make(chan int) var out chan Person junction.New(&out, []junction.Source{{ Input: i, Update: UpdatePersonAge, Model: &Person{Name: "Jeff", Age: 56}, }}) i <- 57 person := <-out older := Person{Age: 57, Name: "Jeff"} if person != older { t.Errorf("Expected %v but got %v", older, person) } }
func Test_Model_lookup_function(t *testing.T) { people := People{ 123: &Person{Name: "Ann", Age: 23}, 456: &Person{Name: "Bob", Age: 21}, } names := make(chan NameUpdate) ages := make(chan AgeUpdate) var out chan Person junction.New(&out, []junction.Source{ { Input: names, Update: ApplyNameUpdate, Model: people.ById, }, { Input: ages, Update: ApplyAgeUpdate, Model: people.ById, }, }) names <- NameUpdate{ id: 123, name: "Anne", } anne := <-out if anne != (Person{Name: "Anne", Age: 23}) { t.Error("Name update failed") } ages <- AgeUpdate{id: 123} anne = <-out if anne.Age != 24 { t.Error("Age update failed") } if anne.Name != "Anne" { t.Error("Name update was lost") } ages <- AgeUpdate{id: 456} bob := <-out if bob != (Person{Name: "Bob", Age: 22}) { t.Error("Age update failed") } // check state if *people[123] != (Person{Name: "Anne", Age: 24}) { t.Errorf("Wrong value for Anne: %v", people[123]) } if *people[456] != (Person{Name: "Bob", Age: 22}) { t.Errorf("Wrong value for Bob: %v", people[456]) } }