github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/webdav/types_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2024, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package webdav
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/e154/smart-home/common"
    25  	m "github.com/e154/smart-home/models"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestExtractScriptName(t *testing.T) {
    30  
    31  	name := extractScriptName("/webdav/scripts/foo.ts")
    32  	require.Equal(t, "foo", name)
    33  
    34  	name = extractScriptName("/webdav/scripts/bar.js")
    35  	require.Equal(t, "bar", name)
    36  
    37  	name = extractScriptName("/webdav/scripts/foo.coffee")
    38  	require.Equal(t, "foo", name)
    39  
    40  	name = extractScriptName("/webdav/scripts/lib.d.ts")
    41  	require.Equal(t, "lib.d", name)
    42  }
    43  
    44  func TestGetScriptLang(t *testing.T) {
    45  
    46  	lang := getScriptLang("/webdav/scripts/foo.ts")
    47  	require.Equal(t, common.ScriptLangTs, lang)
    48  
    49  	lang = getScriptLang("/webdav/scripts/bar.js")
    50  	require.Equal(t, common.ScriptLangJavascript, lang)
    51  
    52  	lang = getScriptLang("/webdav/scripts/foo.coffee")
    53  	require.Equal(t, common.ScriptLangCoffee, lang)
    54  
    55  	lang = getScriptLang("/webdav/scripts/lib.d.ts")
    56  	require.Equal(t, common.ScriptLangTs, lang)
    57  }
    58  
    59  func TestGetExtension(t *testing.T) {
    60  
    61  	ext := getExtension(&m.Script{
    62  		Lang: common.ScriptLangJavascript,
    63  	})
    64  	require.Equal(t, ".js", ext)
    65  
    66  	ext = getExtension(&m.Script{
    67  		Lang: common.ScriptLangCoffee,
    68  	})
    69  	require.Equal(t, ".coffee", ext)
    70  
    71  	ext = getExtension(&m.Script{
    72  		Lang: common.ScriptLangTs,
    73  	})
    74  	require.Equal(t, ".ts", ext)
    75  
    76  	ext = getExtension(&m.Script{})
    77  	require.Equal(t, ".txt", ext)
    78  }
    79  
    80  func TestGetFileName(t *testing.T) {
    81  
    82  	name := getFileName(&m.Script{
    83  		Name: "foo",
    84  		Lang: common.ScriptLangJavascript,
    85  	})
    86  	require.Equal(t, "foo.js", name)
    87  
    88  	name = getFileName(&m.Script{
    89  		Name: "bar",
    90  		Lang: common.ScriptLangCoffee,
    91  	})
    92  	require.Equal(t, "bar.coffee", name)
    93  
    94  	name = getFileName(&m.Script{
    95  		Name: "foo",
    96  		Lang: common.ScriptLangTs,
    97  	})
    98  	require.Equal(t, "foo.ts", name)
    99  
   100  	name = getFileName(&m.Script{
   101  		Name: "bar",
   102  	})
   103  	require.Equal(t, "bar.txt", name)
   104  }
   105  func TestGetFilePath(t *testing.T) {
   106  
   107  	service := NewScripts(nil)
   108  
   109  	path := service.getFilePath(&m.Script{
   110  		Name: "foo",
   111  		Lang: common.ScriptLangJavascript,
   112  	})
   113  	require.Equal(t, "/webdav/scripts/foo.js", path)
   114  
   115  	path = service.getFilePath(&m.Script{
   116  		Name: "bar",
   117  		Lang: common.ScriptLangCoffee,
   118  	})
   119  	require.Equal(t, "/webdav/scripts/bar.coffee", path)
   120  
   121  	path = service.getFilePath(&m.Script{
   122  		Name: "foo",
   123  		Lang: common.ScriptLangTs,
   124  	})
   125  	require.Equal(t, "/webdav/scripts/foo.ts", path)
   126  
   127  	path = service.getFilePath(&m.Script{
   128  		Name: "bar",
   129  	})
   130  	require.Equal(t, "/webdav/scripts/bar.txt", path)
   131  
   132  }