| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package highlight provides code highlighting. |
| 15 |
package highlight |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
"github.com/gohugoio/hugo/config" |
| 22 |
) |
| 23 |
|
| 24 |
func TestConfig(t *testing.T) { |
| 25 |
c := qt.New(t) |
| 26 |
|
| 27 |
c.Run("applyLegacyConfig", func(c *qt.C) { |
| 28 |
v := config.New() |
| 29 |
v.Set("pygmentsStyle", "hugo") |
| 30 |
v.Set("pygmentsUseClasses", false) |
| 31 |
v.Set("pygmentsCodeFences", false) |
| 32 |
v.Set("pygmentsOptions", "linenos=inline") |
| 33 |
|
| 34 |
cfg := DefaultConfig |
| 35 |
err := ApplyLegacyConfig(v, &cfg) |
| 36 |
c.Assert(err, qt.IsNil) |
| 37 |
c.Assert(cfg.Style, qt.Equals, "hugo") |
| 38 |
c.Assert(cfg.NoClasses, qt.Equals, true) |
| 39 |
c.Assert(cfg.CodeFences, qt.Equals, false) |
| 40 |
c.Assert(cfg.LineNos, qt.Equals, true) |
| 41 |
c.Assert(cfg.LineNumbersInTable, qt.Equals, false) |
| 42 |
}) |
| 43 |
|
| 44 |
c.Run("parseOptions", func(c *qt.C) { |
| 45 |
cfg := DefaultConfig |
| 46 |
opts := "noclasses=true,linenos=inline,linenostart=32,hl_lines=3-8 10-20" |
| 47 |
err := applyOptionsFromString(opts, &cfg) |
| 48 |
|
| 49 |
c.Assert(err, qt.IsNil) |
| 50 |
c.Assert(cfg.NoClasses, qt.Equals, true) |
| 51 |
c.Assert(cfg.LineNos, qt.Equals, true) |
| 52 |
c.Assert(cfg.LineNumbersInTable, qt.Equals, false) |
| 53 |
c.Assert(cfg.LineNoStart, qt.Equals, 32) |
| 54 |
c.Assert(cfg.Hl_Lines, qt.Equals, "3-8 10-20") |
| 55 |
}) |
| 56 |
|
| 57 |
c.Run("applyOptionsFromMap", func(c *qt.C) { |
| 58 |
cfg := DefaultConfig |
| 59 |
err := applyOptionsFromMap(map[string]any{ |
| 60 |
"noclasses": true, |
| 61 |
"lineNos": "inline", // mixed case key, should work after normalization |
| 62 |
"linenostart": 32, |
| 63 |
"hl_lines": "3-8 10-20", |
| 64 |
}, &cfg) |
| 65 |
|
| 66 |
c.Assert(err, qt.IsNil) |
| 67 |
c.Assert(cfg.NoClasses, qt.Equals, true) |
| 68 |
c.Assert(cfg.LineNos, qt.Equals, true) |
| 69 |
c.Assert(cfg.LineNumbersInTable, qt.Equals, false) |
| 70 |
c.Assert(cfg.LineNoStart, qt.Equals, 32) |
| 71 |
c.Assert(cfg.Hl_Lines, qt.Equals, "3-8 10-20") |
| 72 |
}) |
| 73 |
} |