1;;; alchemist-scope-test.el --- 2 3;; Copyright © 2015 Samuel Tonini 4;; 5;; Author: Samuel Tonini <tonini.samuel@gmail.com> 6 7;; This file is not part of GNU Emacs. 8 9;; This program is free software: you can redistribute it and/or modify 10;; it under the terms of the GNU General Public License as published by 11;; the Free Software Foundation, either version 3 of the License, or 12;; (at your option) any later version. 13 14;; This program is distributed in the hope that it will be useful, 15;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17;; GNU General Public License for more details. 18 19;; You should have received a copy of the GNU General Public License 20;; along with this program. If not, see <http://www.gnu.org/licenses/>. 21 22;;; Commentary: 23 24;; 25 26;;; Code: 27 28(require 'test-helper) 29 30(ert-deftest test-inside-string-p () 31 (should (with-temp-buffer 32 (alchemist-mode) 33 (insert " 34defmodule Module.Name do 35 36 @moduledoc \"\"\" 37 ## Examples 38 39 .... 40 \"\"\" 41 42end") 43 (goto-line-non-inter 7) 44 (alchemist-scope-inside-string-p))) 45 (should (not (with-temp-buffer 46 (alchemist-mode) 47 (insert " 48defmodule Module.Name do 49 50 @moduledoc \"\"\" 51 ## Examples 52 53 .... 54 \"\"\" 55 56end") 57 (goto-line-non-inter 3) 58 (alchemist-scope-inside-string-p))))) 59 60(ert-deftest test-inside-module-p () 61 (should (with-temp-buffer 62 (alchemist-mode) 63 (insert " 64defmodule Foo do 65 66end") 67 (goto-line-non-inter 3) 68 (alchemist-scope-inside-module-p))) 69 (should (not (with-temp-buffer 70 (alchemist-mode) 71 (insert " 72 73defmodule Foo do 74 75end") 76 (goto-line-non-inter 2) 77 (alchemist-scope-inside-module-p))))) 78 79(ert-deftest test-aliases-from-current-module () 80 (should (equal (list '("Phoenix.Router.Scope" "Scope") 81 '("Phoenix.Router.Resource" "Special")) 82 (with-temp-buffer 83 (alchemist-mode) 84 (insert " 85defmodule Phoenix.Router do 86 87 alias Phoenix.Router.Resource, as: Special 88 alias Phoenix.Router.Scope 89 90 @doc false 91 defmacro scope(path, options, do: context) do 92 options = quote do 93 path = unquote(path) 94 case unquote(options) do 95 alias when is_atom(alias) -> [path: path, alias: alias] 96 options when is_list(options) -> Keyword.put(options, :path, path) 97 end 98 end 99 do_scope(options, context) 100 end 101 102end") 103 (alchemist-scope-aliases)))) 104 (should (equal (list '("String.Break" "Break") 105 '("String.Casing" "Casing")) 106 (with-temp-buffer 107 (alchemist-mode) 108 (insert " 109defmodule Test do 110 111 alias String.{Break, Casing} 112 113end") 114 (alchemist-scope-aliases)))) 115 (should (equal (list '("Phoenix.Router.Resource" "Resource") 116 '("Phoenix.Router.Scope" "Scope")) 117 (with-temp-buffer 118 (alchemist-mode) 119 (insert " 120defmodule Phoenix.Router do 121 122 alias Phoenix.Router.{Resource, Scope} 123 124end") 125 (alchemist-scope-aliases)))) 126 (should (equal (list '("Phoenix.Router.Scope" "Scope") 127 '("Phoenix.Router.Resource" "Special") 128 '("List.Chars.BitString" "BitString") 129 '("List.Chars.Integer" "Integer") 130 '("List.Chars.Atom" "Atom") 131 '("List.Chars.Float" "Float")) 132 (with-temp-buffer 133 (alchemist-mode) 134 (insert " 135defmodule Phoenix.Router do 136 137 alias List.Chars.{Atom, Float} 138 alias Phoenix.Router.Resource, as: Special 139 alias List.Chars.{BitString, Integer} 140 alias Phoenix.Router.Scope 141 142end") 143 (alchemist-scope-aliases))))) 144 145(ert-deftest test-scope-module () 146 (should (equal "Phoenix.Router" 147 (with-temp-buffer 148 (alchemist-mode) 149 (insert " 150defmodule Phoenix.Router do 151 152 defmacro scope(path, options, do: context) do 153 options = quote do 154 path = unquote(path) 155 case unquote(options) do 156 alias when is_atom(alias) -> [path: path, alias: alias] 157 options when is_list(options) -> Keyword.put(options, :path, path) 158 end 159 end 160 do_scope(options, context) 161 end 162 163end") 164 (goto-line-non-inter 6) 165 (alchemist-scope-module))))) 166 167(ert-deftest test-scope-module/skip-heredoc () 168 (should (equal "Module.Name" 169 (with-temp-buffer 170 (alchemist-mode) 171 (insert " 172defmodule Module.Name do 173 174 @moduledoc \"\"\" 175 ## Examples 176 177 Phoenix defines the view template at `web/web.ex`: 178 179 defmodule YourApp.Web do 180 def view do 181 quote do 182 use Phoenix.View, root: \"web/templates\" 183 184 # Import common functionality 185 import YourApp.Router.Helpers 186 187 # Use Phoenix.HTML to import all HTML functions (forms, tags, etc) 188 use Phoenix.HTML 189 end 190 end 191 192 # ... 193 end 194 \"\"\" 195 196end") 197 (goto-line-non-inter 12) 198 (alchemist-scope-module))))) 199 200(ert-deftest test-scope-module/nested-modules () 201 (should (equal "Inside" 202 (with-temp-buffer 203 (alchemist-mode) 204 (insert " 205defmodule Outside do 206 defmodule Inside do 207 208 end 209end") 210 (goto-line-non-inter 4) 211 (alchemist-scope-module))))) 212 213(ert-deftest test-scope-use-modules () 214 (should (equal '("GenServer" "Behaviour") 215 (with-temp-buffer 216 (alchemist-mode) 217 (insert " 218defmodule Phoenix.Router do 219 220 use GenServer 221 use Behaviour 222 223end") 224 (goto-line-non-inter 6) 225 (alchemist-scope-use-modules))))) 226 227(ert-deftest test-scope-use-modules/nested-modules () 228 (should (equal '("Macro" "Nice.Macro") 229 (with-temp-buffer 230 (alchemist-mode) 231 (insert " 232defmodule Phoenix.Router do 233 234 use GenServer 235 use Behaviour 236 237 defmodule Parser do 238 239 use Macro 240 use Nice.Macro 241 end 242 243end") 244 (goto-line-non-inter 12) 245 (alchemist-scope-use-modules))))) 246 247(ert-deftest test-scope-import-modules () 248 (should (equal '("Test" "ExUnit") 249 (with-temp-buffer 250 (alchemist-mode) 251 (insert " 252defmodule Phoenix.Router do 253 254 import Test 255 import ExUnit 256 import Mix.Generator 257 258end") 259 (goto-line-non-inter 6) 260 (alchemist-scope-import-modules))))) 261 262(ert-deftest test-scope-import-modules/nested-modules () 263 (should (equal '("Love") 264 (with-temp-buffer 265 (alchemist-mode) 266 (insert " 267defmodule Phoenix.Router do 268 269 import Test 270 import ExUnit 271 272 defmodule Parser do 273 274 import Love 275 276 end 277 278end") 279 (goto-line-non-inter 10) 280 (alchemist-scope-import-modules))))) 281 282(ert-deftest test-scope-extract-module () 283 (should (equal (alchemist-scope-extract-function ":gen_tcp.accept") 284 "accept")) 285 (should (equal (alchemist-scope-extract-function ":erlang") 286 nil)) 287 (should (equal (alchemist-scope-extract-function "List.duplicate") 288 "duplicate")) 289 (should (equal (alchemist-scope-extract-function "_duplicate") 290 "_duplicate")) 291 (should (equal (alchemist-scope-extract-function "_duplicated?") 292 "_duplicated?")) 293 (should (equal (alchemist-scope-extract-function "parse!") 294 "parse!")) 295 (should (equal (alchemist-scope-extract-function "Enum.take!") 296 "take!")) 297 (should (equal (alchemist-scope-extract-function "String.Chars.impl_for") 298 "impl_for")) 299 (should (equal (alchemist-scope-extract-function "String.Chars.Atom") 300 nil))) 301 302(ert-deftest test-scope-extract-module () 303 (should (equal (alchemist-scope-extract-module ":gen_tcp.accept") 304 ":gen_tcp")) 305 (should (equal (alchemist-scope-extract-module ":erlang") 306 ":erlang")) 307 (should (equal (alchemist-scope-extract-module "List.duplicate") 308 "List")) 309 (should (equal (alchemist-scope-extract-module "Whatever._duplicate") 310 "Whatever")) 311 (should (equal (alchemist-scope-extract-module "Module.read!") 312 "Module")) 313 (should (equal (alchemist-scope-extract-module "String.Chars.impl_for") 314 "String.Chars")) 315 (should (equal (alchemist-scope-extract-module "String.Chars.Atom") 316 "String.Chars.Atom")) 317 (should (equal (alchemist-scope-extract-module "String.Chars.") 318 "String.Chars")) 319 (should (equal (alchemist-scope-extract-module "String.concat") 320 "String")) 321 (should (equal (alchemist-scope-extract-module "to_string") 322 nil))) 323 324(ert-deftest test-scope-alias-full-path () 325 (should (equal "Phoenix.Router.Scope" 326 (with-temp-buffer 327 (alchemist-mode) 328 (insert " 329defmodule Phoenix.Router do 330 331 alias Phoenix.Router, as: Special 332 333end") 334 (alchemist-scope-alias-full-path "Special.Scope")))) 335 (should (equal "Phoenix.Endpoint.Watcher.Everywhere" 336 (with-temp-buffer 337 (alchemist-mode) 338 (insert " 339defmodule Foo do 340 341 alias Phoenix.Endpoint.Watcher 342 343end") 344 (alchemist-scope-alias-full-path "Watcher.Everywhere")))) 345 (should (equal "List" 346 (with-temp-buffer 347 (alchemist-mode) 348 (insert " 349defmodule Foo do 350 351 alias List, as: LT 352 353end") 354 (alchemist-scope-alias-full-path "LT")))) 355 (should (equal "def" 356 (with-temp-buffer 357 (alchemist-mode) 358 (insert " 359defmodule Foo do 360 361 alias Phoenix.Endpoint.Watcher 362 363end") 364 (alchemist-scope-alias-full-path "def")))) 365 (should (equal nil 366 (with-temp-buffer 367 (alchemist-mode) 368 (insert " 369defmodule Foo do 370 371 alias Phoenix.Endpoint.Watcher 372 373end") 374 (alchemist-scope-alias-full-path ""))))) 375 376(provide 'alchemist-scope-test) 377 378;;; alchemist-scope-test.el ends here 379