Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fc1943s committed Jun 25, 2024
1 parent 5c1774b commit f921525
Show file tree
Hide file tree
Showing 154 changed files with 212,804 additions and 177,567 deletions.
2 changes: 1 addition & 1 deletion apps/builder/Builder.dib
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let inline buildProject runtime outputDir path = async {
let! exitCode, _result =
SpiralRuntime.execution_options (fun x ->
{ x with
l1 = command
l0 = command
l6 = Some fileDir
}
)
Expand Down
92 changes: 39 additions & 53 deletions apps/builder/Builder.dib.html

Large diffs are not rendered by default.

92 changes: 39 additions & 53 deletions apps/builder/Builder.dib.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/builder/Builder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Builder =
let! exitCode, _result =
SpiralRuntime.execution_options (fun x ->
{ x with
l1 = command
l0 = command
l6 = Some fileDir
}
)
Expand Down
138 changes: 101 additions & 37 deletions apps/parser/DibParser.dib
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ open Lib

open Common
open FParsec
open SpiralFileSystem.Operators

#!markdown

Expand Down Expand Up @@ -185,13 +186,50 @@ a", (), Position ("", 14, 7, 1))

#!markdown

## Output

#!fsharp

type Output =
| Fs
| Md
| Spi
| Spir

#!markdown

## Magic

#!fsharp

type Magic =
| Fsharp
| Markdown
| Spiral of Output
| Magic of string

#!markdown

## kernelOutputs

#!fsharp

let inline kernelOutputs magic =
match magic with
| Fsharp -> [ Fs ]
| Markdown -> [ Md ]
| Spiral output -> [ output ]
| _ -> []

#!markdown

## Block

#!fsharp

type Block =
{
magic : string
magic : Magic
content : string
}

Expand All @@ -206,6 +244,21 @@ let block =
magicCommand
content
(fun magic content ->
let magic, content =
match magic with
| "fsharp" -> Fsharp, content
| "markdown" -> Markdown, content
| "spiral" ->
let output = if content |> SpiralSm.contains "//// real\n" then Spir else Spi
let content =
if output = Spi
then content
else
content
|> SpiralSm.replace "//// real\n\n" ""
|> SpiralSm.replace "//// real\n" ""
Spiral output, content
| magic -> magic |> Magic, content
{
magic = magic
content = content
Expand All @@ -225,7 +278,7 @@ a
|> run block
|> _assertEqual (
Success (
{ magic = "magic"; content = "a" },
{ magic = Magic "magic"; content = "a" },
(),
Position ("", 14, 7, 1)
)
Expand Down Expand Up @@ -260,8 +313,8 @@ b
|> _assertEqual (
Success (
[
{ magic = "magic1"; content = "a" }
{ magic = "magic2"; content = "b" }
{ magic = Magic "magic1"; content = "a" }
{ magic = Magic "magic2"; content = "b" }
],
(),
Position ("", 26, 9, 1)
Expand All @@ -270,32 +323,13 @@ b

#!markdown

## Output

#!fsharp

type Output =
| Fs
| Md
| Spi
| Spir

let inline kernelOutputs magic =
match magic with
| "fsharp" -> [ Fs ]
| "markdown" -> [ Md ]
| "spiral" -> [ Spi; Spir ]
| _ -> []

#!markdown

## formatBlock

#!fsharp

let inline formatBlock output (block : Block) =
match output, block with
| output, { magic = "markdown"; content = content } ->
| output, { magic = Markdown; content = content } ->
let markdownComment =
match output with
| Spi | Spir -> "/// "
Expand All @@ -310,18 +344,20 @@ let inline formatBlock output (block : Block) =
| line -> System.Text.RegularExpressions.Regex.Replace (line, "^\\s*", $"$&{markdownComment}")
)
|> SpiralSm.concat "\n"
| Fs, { magic = "fsharp"; content = content } ->
| Fs, { magic = Fsharp; content = content } ->
let trimmedContent = content |> SpiralSm.trim
if trimmedContent |> SpiralSm.starts_with "//// test" || trimmedContent |> SpiralSm.starts_with "//// ignore"
if trimmedContent |> SpiralSm.contains "//// test\n"
|| trimmedContent |> SpiralSm.contains "//// ignore\n"
then ""
else
content
|> SpiralSm.split "\n"
|> Array.filter (SpiralSm.trim_start [||] >> SpiralSm.starts_with "#r" >> not)
|> SpiralSm.concat "\n"
| (Spi | Spir), { magic = "spiral"; content = content } ->
| (Spi | Spir), { magic = Spiral output'; content = content } when output' = output ->
let trimmedContent = content |> SpiralSm.trim
if trimmedContent |> SpiralSm.starts_with "//// test" || trimmedContent |> SpiralSm.starts_with "//// ignore"
if trimmedContent |> SpiralSm.contains "//// test\n"
|| trimmedContent |> SpiralSm.contains "//// ignore\n"
then ""
else content
| _ -> ""
Expand Down Expand Up @@ -377,7 +413,7 @@ let inline formatBlocks output blocks =
(list, (None, []))
||> List.foldBack (fun (block, content) (lastMagic, acc) ->
let lineBreak =
if block.magic = "markdown" && lastMagic <> Some "markdown" && lastMagic <> None
if block.magic = Markdown && lastMagic <> Some Markdown && lastMagic <> None
then ""
else "\n"
Some block.magic, $"{content}{lineBreak}" :: acc
Expand Down Expand Up @@ -453,11 +489,11 @@ let inline parse output input =
let blocks =
blocks
|> List.filter (fun block ->
block.magic |> kernelOutputs |> List.contains output || block.magic = "markdown"
block.magic |> kernelOutputs |> List.contains output || block.magic = Markdown
)

match blocks with
| { magic = "markdown"; content = content } :: _
| { magic = Markdown; content = content } :: _
when output = Fs
&& content |> SpiralSm.starts_with "# "
&& content |> SpiralSm.ends_with ")"
Expand Down Expand Up @@ -491,7 +527,7 @@ let inline parse output input =
| false, _ when singleQuoteLine () ->
$" {line}" :: lines, true

| false, _ when line |> SpiralSm.starts_with "#" && block.magic = "fsharp" ->
| false, _ when line |> SpiralSm.starts_with "#" && block.magic = Fsharp ->
line :: lines, false

| false, _ ->
Expand All @@ -518,7 +554,7 @@ let inline parse output input =

let moduleBlock =
{
magic = "fsharp"
magic = Fsharp
content =
$"#if !INTERACTIVE
namespace {namespaceName}
Expand Down Expand Up @@ -585,11 +621,17 @@ let x = 1

//// test

inl x = 0i32
inl x = 1i32

\#!spiral

//// real

inl x = 2i32

\#!spiral

inl x = 0i32
inl x = 3i32

\#!markdown

Expand Down Expand Up @@ -734,7 +776,24 @@ example1
|> _assertEqual "/// # TestModule (TestNamespace)

/// ## ParserLibrary
inl x = 0i32
inl x = 3i32

/// ### TextInput
"

#!fsharp

//// test

example1
|> parse Spir
|> Result.toOption
|> Option.get
|> (formatBlocks Spir)
|> _assertEqual "/// # TestModule (TestNamespace)

/// ## ParserLibrary
inl x = 2i32

/// ### TextInput
"
Expand Down Expand Up @@ -766,7 +825,12 @@ let inline writeDibCode output path = async {
(fun () -> "writeDibCode")
(fun () -> $"output: {output} / path: {path} / {_locals ()}")
let! result = parseDibCode output path
let outputPath = path |> SpiralSm.replace ".dib" $".{output |> string |> SpiralSm.to_lower}"
let pathDir = path |> System.IO.Path.GetDirectoryName
let fileNameWithoutExt =
match output, path |> System.IO.Path.GetFileNameWithoutExtension with
| Spir, fileNameWithoutExt -> $"real_{fileNameWithoutExt}"
| _, fileNameWithoutExt -> fileNameWithoutExt
let outputPath = pathDir </> $"{fileNameWithoutExt}.{output |> string |> SpiralSm.to_lower}"
do! result |> SpiralFileSystem.write_all_text_async outputPath
}

Expand Down
Loading

0 comments on commit f921525

Please sign in to comment.