|
| 1 | +package jasco |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gocraft/web" |
| 5 | + . "github.com/smartystreets/goconvey/convey" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPathParams(t *testing.T) { |
| 10 | + Convey("Given a PathParams", t, func() { |
| 11 | + p := PathParams{ |
| 12 | + req: &web.Request{ |
| 13 | + PathParams: map[string]string{ |
| 14 | + "str": "value", |
| 15 | + "int": "10", |
| 16 | + "neg_int": "-10", |
| 17 | + }, |
| 18 | + }, |
| 19 | + } |
| 20 | + |
| 21 | + Convey("when getting an optional string value", func() { |
| 22 | + s := p.String("str", "default") |
| 23 | + |
| 24 | + Convey("it should return the stored value", func() { |
| 25 | + So(s, ShouldEqual, "value") |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + Convey("when getting a nonexistent string value", func() { |
| 30 | + s := p.String("nonexistent_str", "default") |
| 31 | + |
| 32 | + Convey("it should return the default value", func() { |
| 33 | + So(s, ShouldEqual, "default") |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + Convey("when getting a required string value", func() { |
| 38 | + s, err := p.RequiredString("str") |
| 39 | + |
| 40 | + Convey("it should succeed", func() { |
| 41 | + So(err, ShouldBeNil) |
| 42 | + }) |
| 43 | + |
| 44 | + Convey("it should return the stored value", func() { |
| 45 | + So(s, ShouldEqual, "value") |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + Convey("when getting a required but nonexistent string value", func() { |
| 50 | + _, err := p.RequiredString("nonexistent_str") |
| 51 | + |
| 52 | + Convey("it should fail", func() { |
| 53 | + So(err, ShouldNotBeNil) |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + Convey("when getting an optional positive integer value", func() { |
| 58 | + i, err := p.Int("int", 20) |
| 59 | + |
| 60 | + Convey("it should succeed", func() { |
| 61 | + So(err, ShouldBeNil) |
| 62 | + }) |
| 63 | + |
| 64 | + Convey("it should return the stored value", func() { |
| 65 | + So(i, ShouldEqual, 10) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + Convey("when getting a nonexistent positive integer value", func() { |
| 70 | + i, err := p.Int("nonexistent_int", 20) |
| 71 | + |
| 72 | + Convey("it should succeed", func() { |
| 73 | + So(err, ShouldBeNil) |
| 74 | + }) |
| 75 | + |
| 76 | + Convey("it should return the default value", func() { |
| 77 | + So(i, ShouldEqual, 20) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + Convey("when getting a required positive integer value", func() { |
| 82 | + i, err := p.RequiredInt("int") |
| 83 | + |
| 84 | + Convey("it should succeed", func() { |
| 85 | + So(err, ShouldBeNil) |
| 86 | + }) |
| 87 | + |
| 88 | + Convey("it should return the stored value", func() { |
| 89 | + So(i, ShouldEqual, 10) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + Convey("when getting a required but nonexistent positive integer value", func() { |
| 94 | + _, err := p.RequiredInt("nonexistent_int") |
| 95 | + |
| 96 | + Convey("it should fail", func() { |
| 97 | + So(err, ShouldNotBeNil) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + Convey("when getting a negative integer", func() { |
| 102 | + _, err := p.Int("neg_int", 20) |
| 103 | + |
| 104 | + Convey("it should fail", func() { |
| 105 | + So(err, ShouldNotBeNil) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + Convey("when getting a string as an integer", func() { |
| 110 | + _, err := p.Int("str", 10) |
| 111 | + |
| 112 | + Convey("it should fail", func() { |
| 113 | + So(err, ShouldNotBeNil) |
| 114 | + }) |
| 115 | + }) |
| 116 | + }) |
| 117 | +} |
0 commit comments