clone

an offline map explorer where each world, from a game map to a city's open data, ships as one .atlas file

main
atlas / internal / enrich / enrichers / lenses / lenses_test.go
lenses_test.go 3.9K
1package lenses23import (4 "testing"56 "github.com/FelineStateMachine/atlas/internal/enrich"7)89// offers is a lens offer surface a test states outright.10type offers map[string][]enrich.Lens1112func (o offers) Offers(world string) []enrich.Lens { return o[world] }1314func volume(attached ...enrich.Lens) *enrich.Volume {15 return &enrich.Volume{16 Slug: "cyberpunk-2077",17 Worlds: []enrich.World{{Slug: "night-city", Lenses: attached}},18 }19}2021func run(t *testing.T, v *enrich.Volume, offered offers) enrich.Contribution {22 t.Helper()23 contribution, err := New().Enrich(v, enrich.Context{Lenses: offered})24 if err != nil {25 t.Fatal(err)26 }27 return contribution28}2930func TestAPictureAttachesToTheGroundItPictures(t *testing.T) {31 native := enrich.Lens{Name: "Default", TileSet: "cbp", Stamp: "aaa"}32 warped := enrich.Lens{Name: "IGN Wiki", TileSet: "aligned-night-city",33 Stamp: "bbb", AlignedWith: "cbp"}3435 cases := []struct {36 what string37 attached []enrich.Lens38 offered []enrich.Lens39 attaches int40 }{41 {42 what: "a picture the ground does not have",43 attached: []enrich.Lens{native},44 offered: []enrich.Lens{warped},45 attaches: 1,46 },47 {48 what: "the same picture, offered again",49 attached: []enrich.Lens{native, warped},50 offered: []enrich.Lens{warped},51 },52 {53 what: "the same picture, re-derived",54 attached: []enrich.Lens{native, warped},55 offered: []enrich.Lens{{Name: "IGN Wiki", TileSet: "aligned-night-city", Stamp: "ccc", AlignedWith: "cbp"}},56 attaches: 1,57 },58 {59 what: "an offer with nothing behind it",60 attached: []enrich.Lens{native},61 offered: []enrich.Lens{{Name: "Nothing"}},62 },63 {64 what: "two pictures at once",65 attached: []enrich.Lens{native},66 offered: []enrich.Lens{warped,67 {Name: "Night", TileSet: "cbp-night", Stamp: "ddd"}},68 attaches: 2,69 },70 }7172 for _, c := range cases {73 t.Run(c.what, func(t *testing.T) {74 v := volume(c.attached...)75 contribution := run(t, v, offers{"night-city": c.offered})76 if len(contribution.Ops) != c.attaches {77 t.Fatalf("attached %d pictures, want %d", len(contribution.Ops), c.attaches)78 }79 if err := enrich.Apply(v, contribution); err != nil {80 t.Fatalf("apply: %v", err)81 }82 // Whatever happened, no picture was taken away.83 for _, standing := range c.attached {84 found := false85 for _, lens := range v.Worlds[0].Lenses {86 if lens.Name == standing.Name {87 found = true88 }89 }90 if !found {91 t.Errorf("lens %q was removed", standing.Name)92 }93 }94 })95 }96}9798func TestTheStampTravelsWithThePicture(t *testing.T) {99 v := volume(enrich.Lens{Name: "Default", TileSet: "cbp"})100 contribution := run(t, v, offers{"night-city": {101 {Name: "IGN Wiki", TileSet: "aligned", Stamp: "derivation-stamp", AlignedWith: "cbp"},102 }})103 if err := enrich.Apply(v, contribution); err != nil {104 t.Fatal(err)105 }106 for _, lens := range v.Worlds[0].Lenses {107 if lens.Name != "IGN Wiki" {108 continue109 }110 if lens.Stamp != "derivation-stamp" || lens.AlignedWith != "cbp" {111 t.Errorf("the attached picture says %+v", lens)112 }113 return114 }115 t.Error("the picture did not attach")116}117118func TestAnOfferMayNotRepointASourcesLens(t *testing.T) {119 v := volume(enrich.Lens{Name: "Default", TileSet: "cbp"})120 contribution := run(t, v, offers{"night-city": {{Name: "Default", TileSet: "somewhere-else"}}})121 if len(contribution.Ops) != 1 {122 t.Fatalf("the offer produced %d operations", len(contribution.Ops))123 }124 if err := enrich.Apply(v, contribution); err == nil {125 t.Error("a source's picture was repointed at another pyramid")126 }127}128129func TestNothingOfferedIsNothingSaid(t *testing.T) {130 v := volume(enrich.Lens{Name: "Default", TileSet: "cbp"})131 if contribution := run(t, v, offers{}); !contribution.Empty() {132 t.Error("an empty offer surface produced operations")133 }134 contribution, err := New().Enrich(v, enrich.Context{})135 if err != nil || !contribution.Empty() {136 t.Errorf("no offer surface at all: %v, %d operations", err, len(contribution.Ops))137 }138}