GoÖеĸ߼¶µ¥Ôª²âÊÔģʽ£¨Ï£©

·¢±íÓÚ£º2022-3-04 09:44

×ÖÌ壺´ó ÖРС | ÉÏһƪ | ÏÂһƪ | ÎÒҪͶ¸å

 ×÷ÕߣºÄá¶ú¶à    À´Ô´£ºÏ¡ÍÁ¾ò½ð

¡¡¡¡ÔÚ²âÊÔÖÐʹÓÃÍⲿÊý¾Ý
¡¡¡¡ÔÚGoÖУ¬ÄãÓ¦¸Ã°Ñ²âÊÔµÄÍⲿÊý¾Ý·ÅÔÚÒ»¸ö½Ð×ötestdata µÄĿ¼ÖС£µ±ÄãΪÄãµÄ³ÌÐò¹¹½¨¶þ½øÖÆÎļþʱ£¬testdata Ŀ¼»á±»ºöÂÔ£¬ËùÒÔÄã¿ÉÒÔʹÓÃÕâÖÖ·½·¨À´´æ´¢ÄãÏë²âÊÔ³ÌÐòµÄÊäÈë¡£
¡¡¡¡ÀýÈ磬ÈÃÎÒÃÇдһ¸öº¯Êý£¬´ÓÒ»¸ö¶þ½øÖÆÎļþÉú³Ébase64 ±àÂë¡£
¡¡¡¡func getBase64Encoding(b []byte) string {
¡¡¡¡    return base64.StdEncoding.EncodeToString(b)
¡¡¡¡}

¡¡¡¡ÎªÁ˲âÊÔÕâ¸öº¯Êý²úÉúÕýÈ·µÄÊä³ö£¬ÈÃÎÒÃÇ°ÑһЩÑù±¾ÎļþºÍËüÃÇÏàÓ¦µÄbase64 ±àÂë·ÅÔÚÎÒÃÇÏîÄ¿¸ù²¿µÄtestdata Ŀ¼Ï¡£
¡¡¡¡$ ls testdata
¡¡¡¡img1.jpg img1_base64.txt img2.jpg img2_base64.txt img3.jpg img3_base64.txt

¡¡¡¡ÎªÁ˲âÊÔÎÒÃǵÄgetBase64Encoding() ¹¦ÄÜ£¬ÔËÐÐÏÂÃæµÄ´úÂë¡£
¡¡¡¡func TestGetBase64Encoding(t *testing.T) {
¡¡¡¡    cases := []string{"img1", "img2", "img3"}
¡¡¡¡    for _, v := range cases {
¡¡¡¡        t.Run(v, func(t *testing.T) {
¡¡¡¡            b, err := os.ReadFile(filepath.Join("testdata", v+".jpg"))
¡¡¡¡            if err != nil {
¡¡¡¡                t.Fatal(err)
¡¡¡¡            }
¡¡¡¡            expected, err := os.ReadFile(filepath.Join("testdata", v+"_base64.txt"))
¡¡¡¡            if err != nil {
¡¡¡¡                t.Fatal(err)
¡¡¡¡            }
¡¡¡¡            got := getBase64Encoding(b)
¡¡¡¡            if string(expected) != got {
¡¡¡¡                t.Fatalf("Expected output to be: '%s', but got: '%s'", string(expected), got)
¡¡¡¡            }
¡¡¡¡        })
¡¡¡¡    }
¡¡¡¡}

¡¡¡¡´ÓÎļþϵͳÖжÁȡÿ¸öÑù±¾ÎļþµÄ×Ö½Ú£¬È»ºóËÍÈëgetBase64Encoding() º¯Êý¡£Ëæºó½«Êä³öÓëÔ¤ÆÚÊä³ö½øÐбȽϣ¬Ô¤ÆÚÊä³öÒ²ÊÇ´Ótestdata Ŀ¼ÖлñÈ¡µÄ¡£
¡¡¡¡ÈÃÎÒÃÇͨ¹ýÔÚtestdata Öд´½¨Ò»¸ö×ÓĿ¼£¬Ê¹²âÊÔ¸üÈÝÒ×ά»¤¡£ÔÚÎÒÃǵÄ×ÓĿ¼ÖУ¬ÎÒÃǽ«Ìí¼ÓËùÓеÄÊäÈëÎļþ£¬ÔÊÐíÎÒÃǼòµ¥µØµü´úÿ¸ö¶þ½øÖÆÎļþ£¬²¢±È½Ïʵ¼ÊÊä³öºÍÔ¤ÆÚÊä³ö¡£
¡¡¡¡ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔÌí¼Ó¸ü¶àµÄ²âÊÔ°¸Àý£¬¶ø²»ÐèÒª½Ó´¥Ô´´úÂë¡£
¡¡¡¡$ go test -v
¡¡¡¡=== RUN   TestGetBase64Encoding
¡¡¡¡=== RUN   TestGetBase64Encoding/img1
¡¡¡¡=== RUN   TestGetBase64Encoding/img2
¡¡¡¡=== RUN   TestGetBase64Encoding/img3
¡¡¡¡--- PASS: TestGetBase64Encoding (0.04s)
¡¡¡¡    --- PASS: TestGetBase64Encoding/img1 (0.01s)
¡¡¡¡    --- PASS: TestGetBase64Encoding/img2 (0.01s)
¡¡¡¡    --- PASS: TestGetBase64Encoding/img3 (0.01s)
¡¡¡¡PASS
¡¡¡¡ok      github.com/ayoisaiah/random     0.044s

¡¡¡¡Ê¹ÓûƽðÎļþ
¡¡¡¡Èç¹ûÄãÔÚʹÓÃGoÄ£°å£¬×îºÃÊDzâÊÔÒ»ÏÂÉú³ÉµÄÊä³öÓëÔ¤ÆÚµÄÊä³ö£¬ÒÔÈ·ÈÏÄ£°åÊÇ·ñ°´Ô¤ÆÚ¹¤×÷¡£GoÄ£°åͨ³£±È½Ï´ó£¬ËùÒÔ²»½¨ÒéÏñÎÒÃÇÔÚ±¾½Ì³ÌÖÐÆù½ñΪֹËù×öµÄÄÇÑùÔÚÔ´´úÂëÖÐÓ²±àÂëÔ¤ÆÚÊä³ö¡£
¡¡¡¡ÈÃÎÒÃÇÀ´Ì½ÌÖÒ»ÏÂGoÄ£°åµÄÁíÒ»ÖÖ·½·¨£¬Ëü¿ÉÒÔÔÚÏîÄ¿µÄÕû¸öÉúÃüÖÜÆÚÄÚ¼ò»¯²âÊԵıàдºÍά»¤¡£
¡¡¡¡»Æ½ðÎļþÊÇÒ»ÖÖÌØÊâÀàÐ͵ÄÎļþ£¬°üº¬²âÊÔµÄÔ¤ÆÚÊä³ö¡£²âÊÔº¯Êý´Ó»Æ½ðÎļþÖжÁÈ¡£¬½«ÆäÄÚÈÝÓë²âÊÔµÄÔ¤ÆÚÊä³ö½øÐбȽϡ£
¡¡¡¡ÔÚÏÂÃæµÄÀý×ÓÖУ¬ÎÒÃǽ«Ê¹ÓÃhtml/template £¬Éú³ÉÒ»¸öHTML±í¸ñ£¬ÆäÖаüº¬¿â´æÖÐÿ±¾ÊéµÄÒ»ÐС£
¡¡¡¡type Book struct {
¡¡¡¡    Name          string
¡¡¡¡    Author        string
¡¡¡¡    Publisher     string
¡¡¡¡    Pages         int
¡¡¡¡    PublishedYear int
¡¡¡¡    Price         int
¡¡¡¡}
¡¡¡¡var tmpl = `<table class="table">
¡¡¡¡  <thead>
¡¡¡¡    <tr>
¡¡¡¡      <th>Name</th>
¡¡¡¡      <th>Author</th>
¡¡¡¡      <th>Publisher</th>
¡¡¡¡      <th>Pages</th>
¡¡¡¡      <th>Year</th>
¡¡¡¡      <th>Price</th>
¡¡¡¡    </tr>
¡¡¡¡  </thead>
¡¡¡¡  <tbody>
¡¡¡¡    {{ range . }}<tr>
¡¡¡¡      <td>{{ .Name }}</td>
¡¡¡¡      <td>{{ .Author }}</td>
¡¡¡¡      <td>{{ .Publisher }}</td>
¡¡¡¡      <td>{{ .Pages }}</td>
¡¡¡¡      <td>{{ .PublishedYear }}</td>
¡¡¡¡      <td>${{ .Price }}</td>
¡¡¡¡    </tr>{{ end }}
¡¡¡¡  </tbody>
¡¡¡¡</table>
¡¡¡¡`
¡¡¡¡var tpl = template.Must(template.New("table").Parse(tmpl))
¡¡¡¡func generateTable(books []Book, w io.Writer) error {
¡¡¡¡    return tpl.Execute(w, books)
¡¡¡¡}
¡¡¡¡func main() {
¡¡¡¡    books := []Book{
¡¡¡¡        {
¡¡¡¡            Name:          "The Odessa File",
¡¡¡¡            Author:        "Frederick Forsyth",
¡¡¡¡            Pages:         334,
¡¡¡¡            PublishedYear: 1979,
¡¡¡¡            Publisher:     "Bantam",
¡¡¡¡            Price:         15,
¡¡¡¡        },
¡¡¡¡    }
¡¡¡¡    err := generateTable(books, os.Stdout)
¡¡¡¡    if err != nil {
¡¡¡¡        log.Fatal(err)
¡¡¡¡    }
¡¡¡¡}

¡¡¡¡ÉÏÃæµÄgenerateTable() º¯Êý´ÓBook ¶ÔÏóµÄƬ¶ÏÖд´½¨HTML±í¡£ÉÏÃæµÄ´úÂ뽫²úÉúÒÔÏÂÊä³ö¡£
¡¡¡¡$ go run main.go
¡¡¡¡<table class="table">
¡¡¡¡  <thead>
¡¡¡¡    <tr>
¡¡¡¡      <th>Name</th>
¡¡¡¡      <th>Author</th>
¡¡¡¡      <th>Publisher</th>
¡¡¡¡      <th>Pages</th>
¡¡¡¡      <th>Year</th>
¡¡¡¡      <th>Price</th>
¡¡¡¡    </tr>
¡¡¡¡  </thead>
¡¡¡¡  <tbody>
¡¡¡¡    <tr>
¡¡¡¡      <td>The Odessa File</td>
¡¡¡¡      <td>Frederick Forsyth</td>
¡¡¡¡      <td>Bantam</td>
¡¡¡¡      <td>334</td>
¡¡¡¡      <td>1979</td>
¡¡¡¡      <td>$15</td>
¡¡¡¡    </tr>
¡¡¡¡  </tbody>
¡¡¡¡</table>

¡¡¡¡ÎªÁ˲âÊÔÉÏÃæµÄº¯Êý£¬ÎÒÃǽ«²¶»ñʵ¼Ê½á¹û²¢ÓëÔ¤ÆÚ½á¹û½øÐбȽϡ£ÎÒÃǽ«ÏñÉÏÒ»½ÚÄÇÑù°ÑÔ¤ÆÚ½á¹û´æ´¢ÔÚtestdata Ŀ¼ÖУ¬È»¶ø£¬ÎÒÃDZØÐë×öһЩ¸Ä±ä¡£
¡¡¡¡¼ÙÉèÎÒÃÇÔÚÒ»¸ö¿â´æÖÐÓµÓÐÒÔϵÄÊé¼®ÁÐ±í¡£
¡¡¡¡var inventory = []Book{
¡¡¡¡    {
¡¡¡¡        Name:          "The Solitare Mystery",
¡¡¡¡        Author:        "Jostein Gaarder",
¡¡¡¡        Publisher:     "Farrar Straus Giroux",
¡¡¡¡        Pages:         351,
¡¡¡¡        PublishedYear: 1990,
¡¡¡¡        Price:         12,
¡¡¡¡    },
¡¡¡¡    {
¡¡¡¡        Name:          "Also Known As",
¡¡¡¡        Author:        "Robin Benway",
¡¡¡¡        Publisher:     "Walker Books",
¡¡¡¡        Pages:         208,
¡¡¡¡        PublishedYear: 2013,
¡¡¡¡        Price:         10,
¡¡¡¡    },
¡¡¡¡    {
¡¡¡¡        Name:          "Ego Is the Enemy",
¡¡¡¡        Author:        "Ryan Holiday",
¡¡¡¡        Publisher:     "Portfolio",
¡¡¡¡        Pages:         226,
¡¡¡¡        PublishedYear: 2016,
¡¡¡¡        Price:         18,
¡¡¡¡    },
¡¡¡¡}

¡¡¡¡Õâ¸öͼÊéÇåµ¥µÄÔ¤ÆÚÊä³ö½«¿çÔ½Ðí¶àÐУ¬Òò´Ë£¬ºÜÄѽ«Æä×÷Ϊһ¸ö×Ö·û´®×ÖÃæ·ÅÔÚÔ´´úÂëÄÚ¡£
¡¡¡¡<table class="table">
¡¡¡¡  <thead>
¡¡¡¡    <tr>
¡¡¡¡      <th>Name</th>
¡¡¡¡      <th>Author</th>
¡¡¡¡      <th>Publisher</th>
¡¡¡¡      <th>Pages</th>
¡¡¡¡      <th>Year</th>
¡¡¡¡      <th>Price</th>
¡¡¡¡    </tr>
¡¡¡¡  </thead>
¡¡¡¡  <tbody>
¡¡¡¡    <tr>
¡¡¡¡      <td>The Solitaire Mystery</td>
¡¡¡¡      <td>Jostein Gaarder</td>
¡¡¡¡      <td>Farrar Straus Giroux</td>
¡¡¡¡      <td>351</td>
¡¡¡¡      <td>1990</td>
¡¡¡¡      <td>$12</td>
¡¡¡¡    </tr>
¡¡¡¡    <tr>
¡¡¡¡      <td>Also Known As</td>
¡¡¡¡      <td&gt;Robin Benway</td>
¡¡¡¡      <td>Walker Books</td>
¡¡¡¡      <td>308</td>
¡¡¡¡      <td>2013</td>
¡¡¡¡      <td>$10</td>
¡¡¡¡    </tr>
¡¡¡¡    <tr>
¡¡¡¡      <td>Ego Is The Enemy</td>
¡¡¡¡      <td>Ryan Holiday</td>
¡¡¡¡      <td>Portfolio</td>
¡¡¡¡      <td>226</td>
¡¡¡¡      <td>2016</td>
¡¡¡¡      <td>$18</td>
¡¡¡¡    </tr>
¡¡¡¡  </tbody>
¡¡¡¡</table>

¡¡¡¡³ýÁ˶ԽϴóµÄÊä³öʵÓÃÍ⣬һ¸ö»Æ½ðÎļþ¿ÉÒÔ×Ô¶¯¸üкÍÉú³É¡£
¡¡¡¡ËäÈ»¿ÉÒÔдһ¸ö¸¨Öúº¯ÊýÀ´´´½¨ºÍ¸üлƽðÎļþ£¬µ«ÎÒÃÇ¿ÉÒÔÀûÓÃgoldie£¬Ò»¸öרÃÅΪ»Æ½ðÎļþ´´½¨µÄ¹¤¾ß¡£
¡¡¡¡ÓÃÏÂÃæµÄÃüÁî°²×°×îа汾µÄgoldie¡£
¡¡¡¡$ go get -u github.com/sebdah/goldie/v2

¡¡¡¡ÈÃÎÒÃǼÌÐøʹÓÃgoldieÀ´²âÊÔgenerateTable() º¯Êý¡£
¡¡¡¡func TestGenerateTable(t *testing.T) {
¡¡¡¡    var buf bytes.Buffer
¡¡¡¡    err := generateTable(inventory, &buf)
¡¡¡¡    if err != nil {
¡¡¡¡        t.Fatal(err)
¡¡¡¡    }
¡¡¡¡    actual := buf.Bytes()
¡¡¡¡    g := goldie.New(t)
¡¡¡¡    g.Assert(t, "books", actual)
¡¡¡¡}

¡¡¡¡ÉÏÃæµÄ²âÊÔÔÚÒ»¸ö×ֽڵĻº³åÇøÖⶻñÁËgenerateTable() º¯ÊýµÄÊä³ö¡£È»ºó£¬Ëü½«»º³åÇøµÄÄÚÈÝ´«µÝ¸øgoldie ʵÀýµÄAssert() ·½·¨¡£»º³åÇøÖеÄÄÚÈݽ«Óëtestdata Ŀ¼ÖеÄbooks.golden ÎļþµÄÄÚÈݽøÐбȽϡ£
¡¡¡¡×î³õ£¬ÔËÐиòâÊÔ»áʧ°Ü£¬ÒòΪÎÒÃÇ»¹Ã»Óд´½¨books.golden Îļþ¡£
¡¡¡¡$ go test -v
¡¡¡¡=== RUN   TestGenerateTable
¡¡¡¡    main_test.go:48: Golden fixture not found. Try running with -update flag.
¡¡¡¡--- FAIL: TestGenerateTable (0.00s)
¡¡¡¡FAIL
¡¡¡¡exit status 1
¡¡¡¡FAIL    github.com/ayoisaiah/random     0.006s

¡¡¡¡´íÎóÐÅÏ¢½¨ÒéÎÒÃÇÌí¼Ó-update ±êÖ¾£¬Õ⽫Óûº³åÇøµÄÄÚÈÝ´´½¨books.golden Îļþ¡£
¡¡¡¡$ go test -v -update
¡¡¡¡=== RUN   TestGenerateTable
¡¡¡¡--- PASS: TestGenerateTable (0.00s)
¡¡¡¡PASS
¡¡¡¡ok      github.com/ayoisaiah/random     0.006s

¡¡¡¡ÔÚËæºóµÄÔËÐÐÖУ¬ÎÒÃÇÓ¦¸Ãɾ³ý-update ±êÖ¾£¬ÕâÑùÎÒÃǵĻƽðÎļþ¾Í²»»á±»²»¶ÏµØ¸üС£
¡¡¡¡¶ÔÄ£°åµÄÈκθı䶼»áµ¼Ö²âÊÔʧ°Ü¡£ÀýÈ磬Èç¹ûÄ㽫¼Û¸ñ×ֶθüÐÂΪŷԪ¶ø²»ÊÇÃÀÔª£¬Äã»áÁ¢¼´ÊÕµ½Ò»¸ö´íÎó¡£ÕâЩ´íÎóµÄ·¢ÉúÊÇÒòΪgenerateTable() º¯ÊýµÄÊä³ö²»ÔÙ·ûºÏ»Æ½ðÎļþµÄÄÚÈÝ¡£
¡¡¡¡GoldieÌṩÁ˲îÒ칦ÄÜ£¬ÒÔ°ïÖúÄãÔÚÕâЩ´íÎó·¢Éúʱ·¢Ïֱ仯¡£
¡¡¡¡$ go test -v
¡¡¡¡=== RUN   TestGenerateTable
¡¡¡¡    main_test.go:48: Result did not match the golden fixture. Diff is below:
¡¡¡¡        --- Expected
¡¡¡¡        +++ Actual
¡¡¡¡        @@ -18,3 +18,3 @@
¡¡¡¡               <td>1990&lt;/td>
¡¡¡¡        -      <td>$12</td>
¡¡¡¡        +      <td>€12</td>
¡¡¡¡             </tr><tr>
¡¡¡¡        @@ -25,3 +25,3 @@
¡¡¡¡               <td>2013</td>
¡¡¡¡        -      <td>$10</td>
¡¡¡¡        +      <td>€10</td>
¡¡¡¡             </tr><tr>
¡¡¡¡        @@ -32,3 +32,3 @@
¡¡¡¡               <td>2016</td>
¡¡¡¡        -      <td>$18</td>
¡¡¡¡        +      <td>€18</td>
¡¡¡¡             </tr>
¡¡¡¡--- FAIL: TestGenerateTable (0.00s)
¡¡¡¡FAIL
¡¡¡¡exit status 1
¡¡¡¡FAIL    github.com/ayoisaiah/random     0.007s

¡¡¡¡ÔÚÉÏÃæµÄÊä³öÖУ¬¸Ã±ä»¯±»Çå³þµØÍ»³öÏÔʾ¡£ÕâЩ±ä»¯ÊǹÊÒâµÄ£¬ËùÒÔÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÃ-update ±êÖ¾¸üлƽðÎļþ£¬Ê¹ÎÒÃǵIJâÊÔÔÙ´Îͨ¹ý¡£
¡¡¡¡$ go test -v -update
¡¡¡¡=== RUN   TestGenerateTable
¡¡¡¡--- PASS: TestGenerateTable (0.00s)
¡¡¡¡PASS
¡¡¡¡ok      github.com/ayoisaiah/random     0.006s

¡¡¡¡½áÂÛ
¡¡¡¡ÔÚ±¾½Ì³ÌÖУ¬ÎÒÃÇÑо¿ÁËGoÖеÄһЩ¸ß¼¶²âÊÔ¼¼Êõ¡£Ê×ÏÈ£¬ÎÒÃÇÉîÈëÑо¿ÁËÎÒÃǵÄHTTP°ü£¬²¢Ñ§Ï°ÁËÈçºÎÓÃ×Ô¶¨Òå½Ó¿ÚÄ£ÄâÎÒÃǵÄHTTP¿Í»§¶Ë¡£È»ºó£¬ÎÒÃǻعËÁËÈçºÎÔÚ²âÊÔÖÐʹÓÃÍⲿÊý¾Ý²¢Ê¹ÓÃgoldie´´½¨»Æ½ðÎļþ¡£

¡¡¡¡±¾ÎÄÄÚÈݲ»ÓÃÓÚÉÌҵĿµÄ£¬ÈçÉ漰֪ʶ²úȨÎÊÌ⣬ÇëȨÀûÈËÁªÏµ51TestingС±à(021-64471599-8017)£¬ÎÒÃǽ«Á¢¼´´¦Àí
¡¶2023Èí¼þ²âÊÔÐÐÒµÏÖ×´µ÷²é±¨¸æ¡·¶À¼Ò·¢²¼~

¹Ø×¢51Testing

ÁªÏµÎÒÃÇ

¿ì½ÝÃæ°å Õ¾µãµØͼ ÁªÏµÎÒÃÇ ¹ã¸æ·þÎñ ¹ØÓÚÎÒÃÇ Õ¾³¤Í³¼Æ ·¢Õ¹Àú³Ì

·¨ÂɹËÎÊ£ºÉϺ£À¼µÏÂÉʦÊÂÎñËù ÏîÆåÂÉʦ
°æȨËùÓÐ ÉϺ£²©Îª·åÈí¼þ¼¼Êõ¹É·ÝÓÐÏÞ¹«Ë¾ Copyright©51testing.com 2003-2024
ͶËß¼°Òâ¼û·´À¡£ºwebmaster@51testing.com; ÒµÎñÁªÏµ£ºservice@51testing.com 021-64471599-8017

»¦ICP±¸05003035ºÅ

»¦¹«Íø°²±¸ 31010102002173ºÅ