Пример #1
0
func TestNew(t *testing.T) {
	comic1, err := xkcd.GetCurrent()
	if err != nil {
		t.Fatal(err)
	}

	r, w := io.Pipe()

	go func() {
		e := json.NewEncoder(w)
		err = e.Encode(comic1)
		if err != nil {
			t.Fatal(err)
		}
	}()

	comic2, err := xkcd.New(r)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("comic1: ", comic1)
	t.Log("comic2: ", comic2)

	if *comic1 != *comic2 {
		t.Fatal("comic1 and comic2 don't match")
	}
}
Пример #2
0
func ExampleNew() {
	r := strings.NewReader(`{"month": "3", "num": 1190, "link": "http:\/\/geekwagon.net\/projects\/xkcd1190\/", "year": "2013", "news": "", "safe_title": "Time", "transcript": "", "alt": "The end.", "img": "http:\/\/imgs.xkcd.com\/comics\/time.png", "title": "Time", "day": "25"}`)

	comic, err := xkcd.New(r)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Number: %v\n", comic.Num)
	fmt.Printf("Image: %v\n", comic.Img)
	fmt.Printf("Alt Text: %v\n", comic.Alt)
	// Output:
	// Number: 1190
	// Image: http://imgs.xkcd.com/comics/time.png
	// Alt Text: The end.
}