@@ -1862,6 +1862,84 @@ func TestAddGroup(t *testing.T) {
1862
1862
checkStringContains (t , output , "\n Test group\n cmd" )
1863
1863
}
1864
1864
1865
+ func TestWrongGroupFirstLevel (t * testing.T ) {
1866
+ var rootCmd = & Command {Use : "root" , Short : "test" , Run : emptyRun }
1867
+
1868
+ rootCmd .AddGroup (& Group {ID : "group" , Title : "Test group" })
1869
+ // Use the wrong group ID
1870
+ rootCmd .AddCommand (& Command {Use : "cmd" , GroupID : "wrong" , Run : emptyRun })
1871
+
1872
+ defer func () {
1873
+ if recover () == nil {
1874
+ t .Errorf ("The code should have panicked due to a missing group" )
1875
+ }
1876
+ }()
1877
+ _ , err := executeCommand (rootCmd , "--help" )
1878
+ if err != nil {
1879
+ t .Errorf ("Unexpected error: %v" , err )
1880
+ }
1881
+ }
1882
+
1883
+ func TestWrongGroupNestedLevel (t * testing.T ) {
1884
+ var rootCmd = & Command {Use : "root" , Short : "test" , Run : emptyRun }
1885
+ var childCmd = & Command {Use : "child" , Run : emptyRun }
1886
+ rootCmd .AddCommand (childCmd )
1887
+
1888
+ childCmd .AddGroup (& Group {ID : "group" , Title : "Test group" })
1889
+ // Use the wrong group ID
1890
+ childCmd .AddCommand (& Command {Use : "cmd" , GroupID : "wrong" , Run : emptyRun })
1891
+
1892
+ defer func () {
1893
+ if recover () == nil {
1894
+ t .Errorf ("The code should have panicked due to a missing group" )
1895
+ }
1896
+ }()
1897
+ _ , err := executeCommand (rootCmd , "child" , "--help" )
1898
+ if err != nil {
1899
+ t .Errorf ("Unexpected error: %v" , err )
1900
+ }
1901
+ }
1902
+
1903
+ func TestWrongGroupForHelp (t * testing.T ) {
1904
+ var rootCmd = & Command {Use : "root" , Short : "test" , Run : emptyRun }
1905
+ var childCmd = & Command {Use : "child" , Run : emptyRun }
1906
+ rootCmd .AddCommand (childCmd )
1907
+
1908
+ rootCmd .AddGroup (& Group {ID : "group" , Title : "Test group" })
1909
+ // Use the wrong group ID
1910
+ rootCmd .SetHelpCommandGroupID ("wrong" )
1911
+
1912
+ defer func () {
1913
+ if recover () == nil {
1914
+ t .Errorf ("The code should have panicked due to a missing group" )
1915
+ }
1916
+ }()
1917
+ _ , err := executeCommand (rootCmd , "--help" )
1918
+ if err != nil {
1919
+ t .Errorf ("Unexpected error: %v" , err )
1920
+ }
1921
+ }
1922
+
1923
+ func TestWrongGroupForCompletion (t * testing.T ) {
1924
+ var rootCmd = & Command {Use : "root" , Short : "test" , Run : emptyRun }
1925
+ var childCmd = & Command {Use : "child" , Run : emptyRun }
1926
+ rootCmd .AddCommand (childCmd )
1927
+
1928
+ rootCmd .AddGroup (& Group {ID : "group" , Title : "Test group" })
1929
+ // Use the wrong group ID
1930
+ rootCmd .SetCompletionCommandGroupID ("wrong" )
1931
+
1932
+ defer func () {
1933
+ if recover () == nil {
1934
+ t .Errorf ("The code should have panicked due to a missing group" )
1935
+ }
1936
+ }()
1937
+ _ , err := executeCommand (rootCmd , "--help" )
1938
+ if err != nil {
1939
+ t .Errorf ("Unexpected error: %v" , err )
1940
+ }
1941
+ }
1942
+
1865
1943
func TestSetOutput (t * testing.T ) {
1866
1944
c := & Command {}
1867
1945
c .SetOutput (nil )
0 commit comments