summary refs log tree commit diff stats
path: root/compiler/sempass2.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2023-12-13 10:29:58 +0100
committerGitHub <noreply@github.com>2023-12-13 10:29:58 +0100
commite51e98997ba0aae748ff51eea8133e83370a7df5 (patch)
tree6df6ec97662bf49d13cda7d33f1b451f9e45aa98 /compiler/sempass2.nim
parentdf6cb645f7834de0c43afe2deb023c3e01093503 (diff)
downloadNim-e51e98997ba0aae748ff51eea8133e83370a7df5.tar.gz
type refactoring: part 2 (#23059)
Diffstat (limited to 'compiler/sempass2.nim')
-rw-r--r--compiler/sempass2.nim24
1 files changed, 9 insertions, 15 deletions
diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim
index 448f4d26a..8fc218955 100644
--- a/compiler/sempass2.nim
+++ b/compiler/sempass2.nim
@@ -24,9 +24,6 @@ when defined(useDfa):
 import liftdestructors
 include sinkparameter_inference
 
-
-import std/options as opt
-
 #[ Second semantic checking pass over the AST. Necessary because the old
    way had some inherent problems. Performs:
 
@@ -94,29 +91,26 @@ const
   errXCannotBeAssignedTo = "'$1' cannot be assigned to"
   errLetNeedsInit = "'let' symbol requires an initialization"
 
-proc getObjDepth(t: PType): Option[tuple[depth: int, root: ItemId]] =
+proc getObjDepth(t: PType): (int, ItemId) =
   var x = t
-  var res: tuple[depth: int, root: ItemId]
-  res.depth = -1
+  result = (-1, default(ItemId))
   var stack = newSeq[ItemId]()
   while x != nil:
     x = skipTypes(x, skipPtrs)
     if x.kind != tyObject:
-      return none(tuple[depth: int, root: ItemId])
+      return (-3, default(ItemId))
     stack.add x.itemId
-    x = x[0]
-    inc(res.depth)
-  res.root = stack[^2]
-  result = some(res)
+    x = x.baseClass
+    inc(result[0])
+  result[1] = stack[^2]
 
 proc collectObjectTree(graph: ModuleGraph, n: PNode) =
   for section in n:
     if section.kind == nkTypeDef and section[^1].kind in {nkObjectTy, nkRefTy, nkPtrTy}:
       let typ = section[^1].typ.skipTypes(skipPtrs)
-      if typ.len > 0 and typ[0] != nil:
-        let depthItem = getObjDepth(typ)
-        if isSome(depthItem):
-          let (depthLevel, root) = depthItem.unsafeGet
+      if typ.kind == tyObject and typ.baseClass != nil:
+        let (depthLevel, root) = getObjDepth(typ)
+        if depthLevel != -3:
           if depthLevel == 1:
             graph.objectTree[root] = @[]
           else:
ss='oid'>a6c8162 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                                                      

        
             




                           

                                     









                                           

                                                       























                                                         

                                                   






                                                       
                                                                                                                                               




                                                                                           
                                                    
                                                       












                                                                                                         
package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"

	"github.com/getwtxt/registry"
)

var apiPostUserCases = []struct {
	name    string
	nick    string
	uri     string
	wantErr bool
}{
	{
		name:    "Known Good User",
		nick:    "gbmor",
		uri:     "https://gbmor.dev/twtxt.txt",
		wantErr: false,
	},
	{
		name:    "Missing URI",
		nick:    "missinguri",
		uri:     "",
		wantErr: true,
	},
	{
		name:    "Missing Nickname",
		nick:    "",
		uri:     "https://example.com/twtxt.txt",
		wantErr: true,
	},
	{
		name:    "Missing URI and Nickname",
		nick:    "",
		uri:     "",
		wantErr: true,
	},
}

func Test_apiPostUser(t *testing.T) {
	initTestConf()
	portnum := fmt.Sprintf(":%v", confObj.Port)
	twtxtCache = registry.NewIndex()

	for _, tt := range apiPostUserCases {
		t.Run(tt.name, func(t *testing.T) {
			params := url.Values{}
			params.Set("url", tt.uri)
			params.Set("nickname", tt.nick)

			req, err := http.NewRequest("POST", "https://localhost"+portnum+"/api/plain/users", strings.NewReader(params.Encode()))
			if err != nil {
				t.Errorf("%v\n", err)
			}

			req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
			rr := httptest.NewRecorder()
			apiEndpointPOSTHandler(rr, req)

			if !tt.wantErr {
				if rr.Code != http.StatusOK {
					t.Errorf("Received unexpected non-200 response: %v\n", rr.Code)
				}
			} else {
				if rr.Code != http.StatusBadRequest {
					t.Errorf("Expected 400 Bad Request, but received: %v\n", rr.Code)
				}
			}
		})
	}
}